A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Custom Class with access to stage or Event Listeners

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    3

    Question 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

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    YES, You can.


    arkitx

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    3
    Quote Originally Posted by arkitx View Post
    YES, You can.


    arkitx
    Clever little fellow, aren't you?

    More precisely, HOW would I do this?

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    3
    No one has an answer to this?

    Is what I'm asking either impossible or way too difficult to preform in AS3?

  5. #5
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    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
    [SIGPIC][/SIGPIC]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center