-
Custom Class with access to stage or Event Listeners
Hello everyone
I have a question for you all today, a question off which the answer will probably slap me in the face. Nonetheless, here it is:
I am making (more like attempting) to create Horizontal Arcade like shooter. The project is going good so far:
I have an enemy, who shoots at me. My own ship, who shoots back at him. I'm able to spawn those enemies in waves, etc..
The problem is though that I've have written a whole bunch of code for just one enemy, a whole bunch of code I'm going to use to spawn a bunch of more different types of enemies (faster/slower shooting, faster/slower movement, etc..) and I'm not that keen on having a hundred different .as files for each type of enemy AND each type of weapon.
A small example of my .as file that extends my movieclip in my main .fla file's library:
Code:
public function ballEnemy(tempX:int ,tempY:int ,scoreTemp:int)
What I want to do is make it something like this:
Code:
public function basicEnemy(enemyTemp:MovieClip, tempX:int ,tempY:int ,scoreTemp:int)
Not actually extending the movieclip but rather make an enemy class on its own!
This way I can add more parameters (enemy speed, bullet speed, etc...) without having to write a whole bunch of code.
The main problem is that I use an Event Listener to check if the movieclip entered the frame to start a loop (in which the movieclips moves at a certain speed and destroys itself when it reaches a certain Y coordinate).
The question now is: is it possible to make working Event Listeners in a custom class that is NOT an extension of any kind of object? Or am I making a huge workaround for something that could be done a lot simpler?
EG: the full code of the 'ballEnemy' class.
Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.*;
public class ballEnemy extends MovieClip
{
private var speed:Number = 4;
private var score:int;
private var fireTimer:Timer;
private var canFire:Boolean = true
public function ballEnemy(tempX:int ,tempY:int ,scoreTemp:int)
{
this.x = tempX;
this.y = tempY;
score = scoreTemp;
this.height = this.height * 1.5;
this.width = this.width * 1.5;
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
fireTimer = new Timer(1200,1);
fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
}
private function loop(e:Event):void
{
this.y += speed;
if (this.y > 640)
{
this.destroy();
}
if (this.x > basicEngine.mainShip.x - 5 && this.x < basicEngine.mainShip.x + 5)
{
fireBullet();
}
}
public function fireBullet():void
{
if (canFire)
{
var bullet:enemyBullet = new enemyBullet(this.x,this.y,10);
stage.addChildAt(bullet, 0);
canFire = false;
fireTimer.start();
}
}
public function destroy():void
{
this.removeEventListener(Event.ENTER_FRAME,loop);
if (stage.contains(this))
{
stage.removeChild(this);
}
}
private function fireTimerHandler(e:TimerEvent):void
{
canFire = true;
}
public function getScore():int
{
return score;
}
}
}
Thank you for reading and please, if you can, try to provide me with an answer
-
-
Originally Posted by arkitx
YES, You can.
arkitx
Clever little fellow, aren't you?
More precisely, HOW would I do this?
-
No one has an answer to this?
Is what I'm asking either impossible or way too difficult to preform in AS3?
-
Dignitary
Why is using an enterframe event a problem?
AS3 is an object oriented language, like java or C++. Gunna be hard to get away from using objects
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|