And I am trying the first exercise, but it is not working out very well. There is supposed to be a ball that bounces around when it reaches the corner of the screen, but it just goes to one corner and disappears off screen. I am not sure what I am doing wrong.
Here is my code:
local xspeed = 100 local yspeed = 150 local ball = display.newCircle(100, 100, 20) ball:setFillColor(.2) ball.strokeWidth = 3 ball:setStrokeColor(0, 0, 0) physics.addBody(ball, "dynamic", {isSensor = true}) ball.gravityScale = 0 ball:setLinearVelocity(xspeed, yspeed) local function bounceBall() if (ball.x \> screenW) then ball:setLinearVelocity(xspeed \* -1, yspeed) elseif (ball.x \> 0) then ball:setLinearVelocity(xspeed, yspeed) end if (ball.y \> screenH) then ball:setLinearVelocity(xspeed, yspeed \* -1) elseif (ball.y \> 0) then ball:setLinearVelocity(xspeed, yspeed) end end Runtime:addEventListener("enterFrame", bounceBall)
If you go to Chapter 1, Vectors it is supposed to look like the first animation you see when you scroll down. It shows a ball bouncing around in a space, but it is not working for me.
Note: this is not written in lua and I am having trouble translating it into Corona.
I hope it is not a problem if I can ask for help translating this code into the Lua language.
I also tried doing this:
local screenH = display.actualContentHeight local screenW = display.actualContentWidth local function bounceBall() if ((ball.x \> screenW) or (ball.x \< 0)) then ball:setLinearVelocity(xspeed \* -1, yspeed) end if ((ball.y \> screenH) or (ball.y \< 0)) then ball:setLinearVelocity(xspeed, yspeed \* -1) end end
One more thing: what coding environment can I use to create these projects?
local xspeed = 100 local yspeed = 100 local function enterFrame( self ) if ((self.x \> right) or (self.x \< left)) then self:setLinearVelocity(xspeed \* -1, yspeed) end if ((self.y \> bottom) or (self.y \< top)) then self:setLinearVelocity(xspeed, yspeed \* -1) end end local ball = ssk.display.newCircle( nil, centerX, centerY, {}, {} ) ball.enterFrame = enterFrame listen("enterFrame", ball) ball:setLinearVelocity( xspeed, yspeed )
Note: this is not written in lua and I am having trouble translating it into Corona.
I hope it is not a problem if I can ask for help translating this code into the Lua language.
I also tried doing this:
local screenH = display.actualContentHeight local screenW = display.actualContentWidth local function bounceBall() if ((ball.x \> screenW) or (ball.x \< 0)) then ball:setLinearVelocity(xspeed \* -1, yspeed) end if ((ball.y \> screenH) or (ball.y \< 0)) then ball:setLinearVelocity(xspeed, yspeed \* -1) end end
One more thing: what coding environment can I use to create these projects?
local xspeed = 100 local yspeed = 100 local function enterFrame( self ) if ((self.x \> right) or (self.x \< left)) then self:setLinearVelocity(xspeed \* -1, yspeed) end if ((self.y \> bottom) or (self.y \< top)) then self:setLinearVelocity(xspeed, yspeed \* -1) end end local ball = ssk.display.newCircle( nil, centerX, centerY, {}, {} ) ball.enterFrame = enterFrame listen("enterFrame", ball) ball:setLinearVelocity( xspeed, yspeed )