Borders

Hi,

How can I set borders so that they will make objects stop moving when they hit the border?

You have whatever code you’re using that moves the object check to see if it’s at the border, then not move it.


function setup()
   dx=1
   x=50
end

function draw()
   x = x + dx
   if ((x >= 100) or (x <= 10)) then dx=0 - dx end
   sprite(yoursprite, x, 100)
end

“dx = 0 - dx” is an easy way to toggle between -1 and 1 (or -5 and 5 or whatever), ie. “reverse direction”. The code above would bounce your sprite back and forth betwen 10 and 100. reacting to other barriers in other places is just extending that - maybe you have a table of “bumpers” and you check to see if you hit any of them, then react accordingly.

Well if you know where your objects are you can see where they are in relation to the screen’s width and height (obtainable via the WIDTH and HEIGHT constants).

Have a look at Mark’s recent response to a similar question here:
http://twolivesleft.com/Codea/Talk/discussion/comment/2954#Comment_2954

Edit: Or Mr. Bortels’ above.

0 - dx???

Isn’t that just the same as -dx? Or does lua have some funny definition of 0 that no-one’s told us mathematicians about?

I hate the unary minus, it killed my father? Dunno. it just came out of my brain that way.

I suspect it’s inlined as constant expression and turned into “-dx” anyway. It would be in any decent C compiler, for sure.

Difference between a Mathematician and an Engineer:

Mathematician: The zero is unnecessary.

Engineer: Sweet, it works.

ah, yeah. i implemented borders in a way that is 100000 times more complicated.

Sweet, it works.