Question

I cant fix this error and when I do it causes another one. I want to be able to hit key "71" and have a new instance of the movieclip added to the stage. any suggestions? Im a novice so probably alot of mistakes...

package {

import flash.display.MovieClip; //imports needed
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.engine.EastAsianJustifier;
import flash.events.KeyboardEvent;
import flash.display.Stage;
public class myJellyFish extends MovieClip {

    private var startScaleX:Number;
    private var startScaleY:Number;
    //private var cliqued:Number;
    private var stayonscreenLeft:Number;
    private var stayonscreenRight:Number;
    private var stayonscreenTop:Number;
    private var stayonscreenBottom:Number;

    private var moveDirection:Number;
    private var speed:Number;
    private var turnspeed:Number;


    public function myJellyFish() {



        startScaleX = this.scaleX;
        startScaleY = this.scaleY;
        stayonscreenBottom = 400;
        stayonscreenRight = 500;
        stayonscreenLeft = 5;
        stayonscreenTop = 5;
        moveDirection = .5;
        speed = Math.random()*10;
        turnspeed = 25;

        this.addEventListener(MouseEvent.ROLL_OVER, scrolledOver);
        this.addEventListener(MouseEvent.ROLL_OUT, scrolledOff);
        this.addEventListener(MouseEvent.CLICK, directionChange);
        this.addEventListener(Event.ENTER_FRAME, life)


        trace("custom class be a working");
        // constructor code
    }

    function myMethod () {
        trace("method also be a workin'");
        }

    private function scrolledOver(Event:MouseEvent):void{
        this.alpha = .5;
        }

    private function scrolledOff(Event:MouseEvent):void{
        this.alpha = 1;
        }
    private function directionChange(e:Event):void{
        moveDirection = moveDirection * -1;
        }
    private function life(e:Event):void{
        if (moveDirection > 0){
            Hmovement();
            }
        if (moveDirection < 0){
            Vmovement();
            }
        }
    private function Vmovement():void{
        this.y += speed;

        if(this.y <= stayonscreenBottom){
            speed = speed * -1;
            this.startScaleY * -1;
            }
        if(this.y >= stayonscreenTop){
            speed = speed * -1;
            this.startScaleY * -1;
            }
        }
        private function Hmovement():void{
            this.x += speed;

            if(this.x >= stayonscreenRight){
                speed = speed * -1;
                }
            if(this.x <= stayonscreenLeft){
                speed = speed * -1;
                }
            }
        private function generate(e:KeyboardEvent):void{

            var movieClip:myJellyFish = new myJellyFish();
            addChild(movieClip); 
            movieClip.x = (Math.random() * 200) + 20; 
            movieClip.y = (Math.random()*200) + 20;
            movieClip.name = "jellyfish";
            }



        public function moreClips (event:KeyboardEvent){            //if that key is "F" it will play the tween
            trace(event.keyCode);

            if (event.keyCode == 71){ 
            generate();
            }

        }

    }//end class

}//end package
Was it helpful?

Solution

First of all, as Rin said, there will be an argument error when calling the generate function.

Secondly, you need to add a KeyboardEvent (ref) listener in order to receive the keyboard events.

The easiest way to listen to keyboard events is to add the listener to the Stage (because the KeyboardEvents will bubble to the Stage no matter where they were triggered.) In order to get a reference to the Stage, you need to wait until your MovieClip has been added to the DisplayList (when your instance of myJellyFish has been added as a child somewhere).

You do this by listening for the Event.ADDED_TO_STAGE event.

// Your constructor
public function myJellyFish() {
    // ...
    // Add event listener which will trigger when 
    // the MovieClip has been added to the DisplayList
    this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
}

protected function handleAddedToStage(e:Event):void {
    // Remove event listener since it's no longer needed
    this.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);

    // You now have a reference to the stage, let's add the KeyboardEvent listener
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moreClips);
}

Edit: fixed typo in removeEventListener.

OTHER TIPS

Your generate function has 1 argument wich is e:KeyboardEvent, since you have function moreClips which alredy has the KeyboardEvent, you don't need the argument for the generate function.

Basicly what you are doing now is calling the generate() function without the argument. All you should do is remove the argument from the function.

private function generate():void{
        var movieClip:myJellyFish = new myJellyFish();
        addChild(movieClip); 
        movieClip.x = (Math.random() * 200) + 20; 
        movieClip.y = (Math.random()*200) + 20;
        movieClip.name = "jellyfish";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top