Creation of a Flash artillery game using Box2D – part 2: removing bullets
2009-10-18 00:00:00 来源:WEB开发网This is the second part of Creation of a Flash artillery game using Box2D… one reader asked for a function to make blocks (bullets) disappear after 10 seconds or any given time.
I found the question interesting, and here I am writing something about removing bullets.
To introduce timed bullets, I only need another class to keep track of passed time and pass a flag to the main class when the bullet “gets old”.
This is the new class called bullet.as
package {
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class bullet extends Sprite {
// setting the time to 10,000 milliseconds = 10 seconds
public var time_count:Timer=new Timer(10000);
// flag to determine if I have to remove the bullet
public var remove_the_bullet=false;
public function bullet() {
// simple time listener
time_count.addEventListener(TimerEvent.TIMER, bullet_is_old);
time_count.start();
}
public function bullet_is_old(event:TimerEvent) {
// removing the time listener
time_count.removeEventListener(TimerEvent.TIMER, bullet_is_old);
// setting the flag to true
remove_the_bullet=true;
}
public function has_to_be_removed():Boolean {
// returning the flag, this is the method I'll call from the main class
return (remove_the_bullet);
}
}
}
更多精彩
赞助商链接