View Full Version : [AS3] addChild woes: a space ship firing bullets
KWarp
09-06-2008, 10:02 AM
I'm recreating the arcade game asteroids for a school project.
Gameplay video:
http://www.youtube.com/watch?v=AZdSurJ3hkI
I've built a class for moving the space ship around, but implementing firing bullets within the class is giving me trouble. I want the bullets added on the same level that the space ship is, but without a _parent property, I can't do that in the scope of my class.
I'm considering two options:
1) Use addChild through the stage property, although that doesn't give me too much control.
2) Dispatching an event and adding an event listener on the stage, but what kind of event would I dispatch? Would I need to make a custom one? This route seems a little more cumbersome than it should be.
Thoughts, comments appreciated.
yogeshpuri
09-06-2008, 10:24 AM
I will suggest to create a controller class that will be on the same level at your Hero and then that class will listen to either your hero class to create bullet or any MouseEvent. This controller class can have a reference of stage when gets created. for example
Code for Hero
public function dispatchEventBulletCreation():void{
dispatchEvent(new Event("CREATE_BULLET"));
}
Code on Timeline or Stage
var myHero:Hero = new Hero();
var myController:Controller = new Controller("you can pass stage here by saying 'this'");
myController.configureListener(myHero,"CREATE_BULLET");
Code for Controller
public var stageMC:MovieClip;
//------Constructor ----
public function Controller(targetMC:MovieClip){
this.stageMC = targetMC;
}
function fnCreateBullet(){
var bullet:Bullet = new Bullet(); // create a diff class for bullet and put all bullet related code there so that it can be disposed when it is out of screen or had hit the target
this.stageMC.addChild(bullet);
}
function configureListener(listenerTarget:MovieClip,EventTo Listen:String){
//---- Here your controller class is listening to Create Bullet event dispatched by Hero
listenerTarget.addEventListener(EventToListen,fnCr eateBullet);
}
Hope it gives some idea
KWarp
09-13-2008, 07:48 AM
I'm having a really hard time figuring out how your code is coordinated, but I get the idea that an event model is necessary. I extended the event class for my purposes.
package {
/*
Class for all relevant events in the Asteroids videogame
Example of how to dispatch the event
dispatchEvent(new AsteroidsEvent(AsteroidsEvent.CREATE_BULLET, "comment"));
How to listen to it
addEventListener(AsteroidsEvent.CREATE_BULLET, functionName);
*/
// import the Event class so it can be extended
import flash.events.Event;
// define the AsteroidsEvent class as a subclass of Event
public class AsteroidsEvent extends Event {
// static string constants used as event types
public static const CREATE_BULLET:String = "create bullet";
public static const DESTROY_BULLET:String = "destroy bullet";
//add others
// internal property defining the nature of the animation
private var _detail:String = "N/A";
// public read-only property for detail
public function get detail():String {
return _detail;
}
// AsteroidsEvent constructor used to create new animation complete events
// when created, the name of the clip calling should be passed after type
public function AsteroidsEvent(type:String, detail:String = "N/A"){
// call super (Event constructor) indicating
// true for bubbles parameter
super(type, true);
// set detail property
_detail = detail;
}
// override clone method so that it can correctly
// return a AsteroidsEvent instance
public override function clone():Event {
return new AsteroidsEvent(type, _detail);
}
}
}
Adobe's developer documentation was a big help. :cool:
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.