0
votes

This is my code that I have so far

package  {

    import flash.display.MovieClip;
    import flash.events.*;

    public class Main extends MovieClip {

        var leftpressed:Boolean = false;
        var rightpressed:Boolean = false;

        public function Main() 
        {
            //constructor code
            Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnpress);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, FlyBullet);      
        }       

        function moveToKey (event:Event) 
        {
            if (leftpressed && (Spaceship.x>0))
            {
                Spaceship.x = -5;
            }
            if (rightpressed && (Spaceship.x<550))
            {
                Spaceship.x = +5;
            }

        }       

        function setKeyPress(e:KeyboardEvent):void 
        {
            if (e.keyCode == 37)
            {
                leftpressed = true;
            }
            if (e.keyCode == 39)
            {
                rightpressed = true;
            }
        }

        function setKeyUnpress(e:KeyboardEvent): void   
        {
            if (e.keyCode == 37)
            { 
                leftpressed = false;
            }
            if (e.keyCode == 39)
            {
                rightpressed = false;
            }

        }

        function FlyBullet (e:KeyboardEvent):void
        {           
            if (e.keyCode == 32)
            var bulletshot:Bullet = new Bullet();
            addChild(bulletshot);
            bulletshot.x = Spaceship.x;
            bulletshot.y = Spaceship.y
        }

    }
}

With the biggest issue with the final FlyBullet function that outputs an the error listed as

TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at Main/FlyBullet()
2
Please specify what you are trying to accomplish (desired output).radu-matei

2 Answers

1
votes

This is your problem:

if  (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y

There are no curly brackets so the if condition only applies to the first line where bulletshot is assigned. In other words, if you press any key other than SPACE it will skip the assignment of bulletshot but still try to addChild(bulletshot), but since bulletshot was not assigned a value it is null and you get errors.

I think you meant this:

if (e.keyCode == Keyboard.SPACE) {
    var bulletshot:Bullet = new Bullet();
    addChild(bulletshot);
    bulletshot.x = Spaceship.x;
    bulletshot.y = Spaceship.y
}

PS: Your code is poorly indented in a lot of places. It helps to have correctly indented code. An auto-formatter could help you here.

1
votes

It is generally considered bad practice to omit brackets in multiline if statements, this is an example why. Whenever e.keyCode != 32 the addChild line will try to run.

Solution would be:

///...in the Main function//
stage.addEventListener(KeyboardEvent.KEY_DOWN, flyBullet);
///...
function flyBullet (e:KeyboardEvent):void
{

    if  (e.keyCode == 32)
    {
        var bulletshot:Bullet = new Bullet();
        addChild(bulletshot);
        bulletshot.x = Spaceship.x;
        bulletshot.y = Spaceship.y;
    }
}

Some unrelated as3 code quality tips, to avoid possible errors later:

I'm guessing Spaceship isn't a static class, so i suggest using lowercase first letters for variable/function names, and uppercase for classes, but this is just good practice and does not affect your codes correctness.

It is also good practice to define the accessibility of your functions, i.e public, private, internal, protected. If you omit it the default is internal.

And only skip brackets after conditionals when necessary.