OLPCities/Shooting in the bad guys: Difference between revisions

From OLPC
Jump to navigation Jump to search
No edit summary
No edit summary
Line 27: Line 27:
shoot=1;
shoot=1;
}
}
...
</pre>

The other interesting thing we are doing is to make the monster to go in the direction of the astronaut. We use the method <b>target</b> to do this.
And we need to change the frames of the sprite according the angle the monster is walking. We use <b>setFrameByDirection</B to do it. Look the lines of code:
<pre>
...
e1.setFrameByDirection(1,45,2,46,134,1,135,224,3,225,314,0,315,360,2);
...
e1.target(av,0,0);
...
...
</pre>
</pre>

Revision as of 17:48, 24 September 2006

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. And we need to change the frames of the sprite according the angle the monster is walking. We use setFrameByDirection</B 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); 
...