I am having a problem with vector coding, I recently started a book called The Nature Of Code:
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.