Making something continually happen ... a repeat?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
DaveyJJ
Posts: 15
Joined: Sat Aug 25, 2007 8:38 pm
Location: Waterloo, Ontario
Contact:

Making something continually happen ... a repeat?

Post by DaveyJJ » Wed Jan 16, 2008 9:49 pm

I want an object to continually move until another condition exists.

repeat
move me relative 0,-50 in 1 second
wait 2 seconds
end repeat

Is that something like the right thing?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Jan 17, 2008 12:18 am

Hi DaveyJJ,

Whether this is the right approach depends on whether you want to be able to do other things while the object is moving.

What exactly is the "condition"?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

DaveyJJ
Posts: 15
Joined: Sat Aug 25, 2007 8:38 pm
Location: Waterloo, Ontario
Contact:

Post by DaveyJJ » Thu Jan 17, 2008 4:28 am

Mark wrote:Hi DaveyJJ,

Whether this is the right approach depends on whether you want to be able to do other things while the object is moving.

What exactly is the "condition"?

Mark
Moving a character ala Kyntt Stories. When it bumps into something (I assume I'll have to use AE for this) it stops. "Wall", "floor" etc. I hold the left or right arrow down and the little animated character moves until it hits an obstacle. More like this I suppose having read a bit more at work today ...

while arrowKey theLeftKey -- left key is already declared as a variable]
move me -1,0
end arrowKey

So maybe not even a repeat, just "while this keyboard input is happening" move me a pixel (or more) at a time.

I still have to figure out how to fake gravity ... I assume always moving the character down a few pixels every bit of time ... and jumping up (then slowly stopping and falling back).

I'm not sure I've ever run across a demo of making a simple platformer in Revolution, even after these many years I've been watching it.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu Jan 17, 2008 1:58 pm

Hi daveJJ,

Make a new stack with two graphics, circles for example. You can also use other objects, but for this example I'm using graphics.

Insert the following code at stack level (card level should work too):

Code: Select all

constant kSpeed = 2
constant kThrottleGrc1 = .75
constant kThrottleGrc2 = 1.5

on preOpenStack
  initialise
  pass preOpenstack
end preOpenStack

on initialise
  set the loc of grc 1 to 220,150
  set the loc of grc 2 to 310,150
   if "checkArrowKey" is not in the pendingMessages then checkArrowKey
end initialise

on checkArrowKey
  put the loc of grc 1 into myLoc1
  put the loc of grc 2 into myLoc2
  put the keysDown into myKeys
  if 65361 is among the items of myKeys then
    subtract kSpeed*kThrottleGrc1 from item 1 of myLoc1
    subtract kSpeed*kThrottleGrc2 from item 1 of myLoc2
  else if 65363 is among the items of myKeys then
    add kSpeed*kThrottleGrc1 to item 1 of myLoc1
    add kSpeed*kThrottleGrc2 to item 1 of myLoc2
  else if 65362 is among the items of myKeys then
    subtract kSpeed*kThrottleGrc1 from item 2 of myLoc1
    subtract kSpeed*kThrottleGrc2 from item 2 of myLoc2
  else if 65364 is among the items of myKeys then
    add kSpeed*kThrottleGrc1 to item 2 of myLoc1
    add kSpeed*kThrottleGrc2 to item 2 of myLoc2
  end if
  lock screen
  set the loc of grc 1 to myLoc1
  set the loc of grc 2 to myLoc2
  unlock screen
  -- don't reduce 20, it will become too processor intensive
  if "checkArrowKey" is not in the pendingMessages then send "checkArrowKey" to me in 20 millisecs
end checkArrowKey

on arrowKey
end arrowKey
Now press an arrow key and the objects should move. If you want to use different objects, you'll need to adjust the script accordingly and if you need only one object, you can do without the throttle variables.

I have also tried the same with an arrowKey handler, but that's way too slow.

You can download the stack at http://economy-x-talk.com/software/animationtest.rev

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

DaveyJJ
Posts: 15
Joined: Sat Aug 25, 2007 8:38 pm
Location: Waterloo, Ontario
Contact:

Post by DaveyJJ » Thu Jan 17, 2008 4:43 pm

Wow, Mark. I will look through this later today and really appreciate the help.

That's exactly right.

Do you think I have to use AE for the collision testing or can I simply use the intersection function to detect when two object begin to overlap? I don't need a great deal of accuracy ... or since AE has a collision detection with a line built in, it still might be better as I can draw lines to use a colliders?

Once I figure this out I can then simply add "falling" automatically when no floor collision is detected (a -Y move every 20 milliseconds) so that, in this example, both balls start moving downward automatically. Or, when the stack starts to run, they fall down by x amount unless the spacebar is held.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Jan 21, 2008 11:33 am

Hi DaveyJJ,

For simple collision detection, you don't need AE. You can simply check whether the bottom of an object is larger than the top of the object that represents the "floor" in your game. If you need more complex collision functions, you might want to use AE.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”