Moving an object around the screen

How can I move my ball all around the screen ? I want it to bounce of the four sides of the screen at random places . How can i do that ?

Does “move” mean with your finger, or via physics simulation, or… ? Does “random places” mean the trajectory of the ball should be random after every bounce, or …?  I’ll just pick the meanings I preferred and if that happened to be what you really meant to ask, then yay :slight_smile:

local physics = require "physics" function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 1.1, friction = 1}) physics.addBody (rightWall, "static", {bounce = 1.1, friction = 1}) physics.addBody (ceiling, "static", {bounce = 1.1, friction = 1}) physics.addBody (floor, "static", {bounce = 1.1, friction = 1}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 1, 1, 0) CreateWalls(1) -- create image of a ball local Ball = display.newCircle(100, 100, 40) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=0}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0)

One ball (circle attached to physics body) bouncing around “sides of the screen” (four rectangles attached to “static” physics bodies so they don’t move).

Do you just want to drag and throw the ball? 

–SonicX278 

Does “move” mean with your finger, or via physics simulation, or… ? Does “random places” mean the trajectory of the ball should be random after every bounce, or …?  I’ll just pick the meanings I preferred and if that happened to be what you really meant to ask, then yay :slight_smile:

local physics = require "physics" function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 1.1, friction = 1}) physics.addBody (rightWall, "static", {bounce = 1.1, friction = 1}) physics.addBody (ceiling, "static", {bounce = 1.1, friction = 1}) physics.addBody (floor, "static", {bounce = 1.1, friction = 1}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 1, 1, 0) CreateWalls(1) -- create image of a ball local Ball = display.newCircle(100, 100, 40) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=0}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0)

One ball (circle attached to physics body) bouncing around “sides of the screen” (four rectangles attached to “static” physics bodies so they don’t move).

Do you just want to drag and throw the ball? 

–SonicX278