Keep It Up (bouncing ball physics) Lingo behaviour code...

I thought I would post the original code from the Keep It Up game from my last post. As the game evolved I added a lot more stuff such as scaling the shadow, disorting the ball when it was kicked and including 'trick kicks' (which was a bit of a nightmare). This is the basic code as version 1. Feel free to use it and add your own twist if you wish. Simply start a new Director project, drag a ball sprite onto the Stage and position a shadow sprite one channel below it in the Score. Add a simple 'go to the frame' behaviour to the Frame and attached the ball behaviour below to the ball sprite. Done!

-- jim's bouncing ball behaviour

property my, shad
property grav, accelH, accelV

on beginsprite me

   my = sprite(me.spritenum) -- sets a variable 'my' to easily refer to this sprite

   grav = 1 -- the gravity, change this to change the physics of the game

   accelH = 0 -- the acceleration in x direction
  
accelV = 0 -- the acceleration in y direction

   shad = sprite(my.spritenum - 1) -- sets 'shad' to the sprite i use as a shadow

   my.locv = 150 -- initial position of the ball. use (sprite(1).bottom - 20 - my.height/2) to place it on the bottom of the screen
  
shad.loch = my.loch -- sets the x position of the shadow to the same as the ball

end

on exitFrame me

   accelV = accelV + grav -- affects gravity on the y acceleration
   accelH = accelH * 0.96 -- dampens the x acceleration

   my.locv = my.locv + accelV -- affects y acceleration on the balls y position
   my.loch = my.loch + accelH -- affects x acceleration on the balls y position

   shad.loch = my.loch -- positions the shadow to the same x location as the ball (you may like to also change the size of the shadow)

   -- tells the ball what to do when it hits the left side of the screen
   if my.loch < (sprite(1).left + 3 + my.width/2) then
      my.
loch = (sprite(1).left + 3 + my.width/2)
      accelH = accelH * -
0.9
   end if

   -- tells the ball what to do when it hits the right side of the screen
   if my.loch > (sprite(1).right - 3 - my.width/2) then
      my.loch = (sprite(1).right - 3 - my.width/2)
      accelH = accelH * -
0.9
  
end if

   -- tells the ball what to do when it hits the top of the screen. you can add this back in if you want the ball to bounce off the top
  
--if my.locv < (sprite(1).top + 3 + my.height/2) then
     
-- my.locv = (sprite(1).top + 3 + my.height/2)
      -- accelV = accelV * -0.8
  
--end if

-- tells the ball what to do when it hits the bottom of the screen
  
if my.locv > (sprite(1).bottom - 20 - my.height/2) then
     
my.locv = (sprite(1).bottom - 20 - my.height/2)
      accelV = accelV * -
0.6
  
end if

   my.rotation = my.rotation + accelH -- rotates the ball based on the x acceleration

end

on mousedown me

   -- works out the x and y accelerations when the user clicks on the ball
  
-- the further from the centre of the ball the harder the kick
  
h = (the mouseh - my.loch) * -1
   v = (
the mousev - my.locv) * -1

   accelV = v
   accelH = h

end

Comments

Gerry Prchal

HI, here is my blog.

Posted on May 25, 2010 02:22 by Gerry Prchal

Trina Creech

Wonderful blog! I saw it at Bing and I must say that entries are well thought of. I will be coming back to see more posts soon.

Posted on May 30, 2010 21:17 by Trina Creech

Silas Suma

You certain do know what youre discussing.  Man, this website is just great!  I cant wait to study far more of what youve got to express.  Im genuinely happy that I came across this when I did due to the fact I had been genuinely starting to have bored with the whole blogging scene.  Youve turned me around, man!

Posted on May 31, 2010 02:51 by Silas Suma

downlaod

Thought you may like to know I clicked your site from the first page of aBing seach. Great Job. I know how tidious it is to get your your website on the first page of a search. Ive downloaded ebook after ebook and finally got <A href="http://nerfgunsforsale.org/">Nerf N Strike Vulcan ebf 25 blaster yellow</A></SPAN></SPAN></SPAN> on the first page. DANG!! only took 6months!!

Posted on June 6, 2010 16:35 by downlaod

donwload

Just thought you might like to know I came to your site from the first page of aAsk seach. Nice work. I know how tidious it is to get your this page on the first page of a search. Ive purchased ebook after ebook and finally got <A href="nerfnstrikevulcanebf25blasteryellow.net/">Nerf Guns</A></SPAN></SPAN></SPAN> on the first page. omg only took 4months!!

Posted on June 8, 2010 07:40 by donwload

Rosamond Kakacek

Here is the second instance I have noticed your web sites within the last couple weeks.  Seems as if I should take a note of it.

Posted on June 16, 2010 06:06 by Rosamond Kakacek

Alfonzo Gundert

Superb article, plenty of fantastic details.  I'm going to point out to my friends and ask them the things they think.

Posted on June 20, 2010 06:27 by Alfonzo Gundert

Natural Cures for BV

Fantastic article, a bunch of valuable material.

Posted on June 23, 2010 08:37 by Natural Cures for BV

text messagingsoftware

Did you know that I clicked your site from the first page of aBing seach. Nice work. I know how tidious it is to get your your website on the first page of a search. Ive purchased program after program and finally got <A href="asbestos-mesothelioma-attorney.us/">lung cancer</A> on the first page. whew!! only took 3months!!

Posted on June 29, 2010 09:18 by text messagingsoftware

text messagingsoftware

Did you know that I clicked your site from the first page of aYahoo seach. Nice work. I know how nerve raking it is to get your your site on the first page of a search. Ive purchased program after program and now got <A href="bestmusclebuildingsupplement.us/.../">nitric oxide supplement</A> on the first page. thank gosh only took 4months!!

Posted on June 29, 2010 09:28 by text messagingsoftware

Sona Acy

Bizarre... I just found your blog by searching for 'financial spreadbetting' on Yahoo. But I haven't found any articles about that on here?

Posted on July 2, 2010 20:08 by Sona Acy

Reid Kaley

I found a link to your site on a Currency trading web site, and I must say... Your web site is a lot better. You explain it more clearly, thanks

Posted on July 3, 2010 03:32 by Reid Kaley

Elmer Blohm

Just wanted to let you know... your site looks really weird in Chameleon on a mac

Posted on July 3, 2010 15:40 by Elmer Blohm

Richie Loges

I was searching for articles about this on Bing and came across your item. I found it to be nice and clear. Thanks

Posted on July 4, 2010 14:45 by Richie Loges

Colby Zolman

I just signed up to your rss feed after reading this article! Can you publish more on the subject in future articles?

Posted on July 5, 2010 15:05 by Colby Zolman

Debora Aramboles

Hello webmaster can I use some of the information from this post if I provide a link back to your site?

Posted on July 6, 2010 02:59 by Debora Aramboles

Shaunte Tian

Just a heads up... your website looks really strange in Mozilla on a Mac

Posted on July 7, 2010 02:24 by Shaunte Tian

recession marketing

I used to have a blog on this subject, but I had so many spam comments I had to shut it. You seem to have a better spam filter! Kudos!

Posted on July 11, 2010 14:10 by recession marketing

Online Chat

Just a heads up... your web site looks very odd in Mozilla on a mac

Posted on July 12, 2010 20:28 by Online Chat

HDTV on PC

I was looking out for articles about this on Bing and found your post. I found it to be very interesting. Thanks

Posted on July 13, 2010 05:24 by HDTV on PC

Johnathan Perreira

Hello webmaster can I use some of the information from this post if I provide a link back to your site?

Posted on July 13, 2010 20:12 by Johnathan Perreira

Emmy Abramek

I was searching for articles about this on Bing and came across your post. I found it to be very interesting. Thanks

Posted on July 14, 2010 03:52 by Emmy Abramek

platinum diamond rings

Just a heads up... your site looks really odd in Firefox on a mac

Posted on July 15, 2010 19:51 by platinum diamond rings

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading