OLPCities/Shooting in the bad guys

From OLPC
< OLPCities
Revision as of 14:04, 24 September 2006 by Adamascj (talk | contribs)
Jump to: navigation, search
Tut7-1.jpg


Look the figure. The astronaut has a gun. And glued to the gun there are a little red bullet. They are in the "Pink planet" and the astronaut can shoot against one of the "echoos", the mosters that live in the planet.

How did we code this?

The sprite "bullet" needs to be "glued" to the sprite "astronaut" aproximately near the gun and having an inferior "Z" to be hided. There are the method follow to do it. At the exercice we have:



 
bl.follow(av,15,   16);

The two number are de coordinates of the bullet "inside" the avatar. At the moment of the shoot (pressing the key "P") we liberate the bullet, difine its direction (the same of the avatar at this moment) and we increase the velocity (2 for the avatar and 5 for the bullet). Look part of the code. This action needs to be "discreet" - only one time - and we use a flag ("shoot") to do this:

...
if(skey.pressed){
 if(shoot==0){
   bl.stopFollowing();
   bl.setDir(av.xdir,av.ydir);
   bl.setSpeed(5);
   shoot=1; 
  }
...

The other interesting thing we are doing is to make the monster to go in the direction of the astronaut. We use the method target to do this.

Echoo1.gif

And we need to change the frames of the sprite according the angle the monster is walking. We use setFrameByDirection to do it. Look the lines of code:

 
...
 e1.setFrameByDirection(1,45,2,46,134,1,135,224,3,225,314,0,315,360,2);	 
...
 e1.target(av,0,0); 
...
Tut7-2.jpg


Pay attention that we need to begin the definition of the directions by the angle 1 until 360.