0

it's my first question here, but I've been having this problem lately and need some help.

I'm programming a small little Android Game for the weekend and I'm having a problem with making my ship's bullets hit the enemies and both disappear...

I'll give you guys a little rundown of my code:

stage.addEventListener(Event.ENTER_FRAME, Update);
LeftButton.addEventListener(TouchEvent.TOUCH_OVER, LBOn);
LeftButton.addEventListener(TouchEvent.TOUCH_BEGIN, LBTouch);
LeftButton.addEventListener(TouchEvent.TOUCH_OUT, LBOff);
LeftButton.addEventListener(TouchEvent.TOUCH_END, LBRelease);
RightButton.addEventListener(TouchEvent.TOUCH_OVER, RBOn);
RightButton.addEventListener(TouchEvent.TOUCH_BEGIN, RBTouch);
RightButton.addEventListener(TouchEvent.TOUCH_OUT, RBOff);
RightButton.addEventListener(TouchEvent.TOUCH_END, RBRelease);
BA.addEventListener(TouchEvent.TOUCH_BEGIN, Shoot);

this is my Stage's Event Listener Block. All the current EventListeners are in this little part, pretty simple and they work okay for what I'm doing, but the last event calls the function Shoot and that's where things get tricky...

function Shoot(TouchEvent) {

        SpawnBullet();

    };

Ok, simple enough. Just a function that calls another function to spawn my bullets, not much harm there the way I see it...

this is how I tried to make the code run:

function SpawnBullet() {

        var Bullet:Bullet1 = new Bullet1();
        stage.addChild(Bullet);

            Bullet.x = Player.x;
            Bullet.y = Player.y - (Player.height / 2);

                Bullet.addEventListener(Event.ENTER_FRAME, BulletUpdate);

                    function BulletUpdate(Event) {

                        Bullet.y -= 20;

                        if(Bullet.y <= 100) {

                            Bullet.removeEventListener(Event.ENTER_FRAME, BulletUpdate);
                            stage.removeChild(Bullet);
                            Bullet = null;

                        };

                    };

    };    

When I run the game i get this error:

ReferenceError: Error #1069: Property ENTER_FRAME not found on flash.events.Event and there is no default value.
at Function/Shooter_fla:MainTimeline/SpawnBullet/Shooter_fla:BulletUpdate()[Shooter_fla.MainTimeline::frame1:41]

I'm not trying to fake my skills, I think I'm not understanding how the adding and removal of childs work, and since I hate just searching the web for a way to do something without learning the way something works I came here for a bit of help. Thanks in advance for any help!

1 Answers1

1

The error has nothing to do with adding/removing children. Your mistake is to declare function argument Event with the same name as the class name thus compiler is confused. Also, as a side note, try to avoid declaring functions inside functions, it's like welcoming pain and horrors.

I think your code will be fine as following:

function SpawnBullet():void
{
    // With the "new" operator you can omit () if constructor has no arguments.
    var aBull:Bullet1 = new Bullet1;

    stage.addChild(aBull);

    aBull.x = Player.x;
    aBull.y = Player.y - (Player.height / 2);

    aBullet.addEventListener(Event.ENTER_FRAME, BulletUpdate);
}

function BulletUpdate(e:Event):void
{
    // Get the reference to the bullet from the event.
    // Alternately you might store it in a variable outside of both functions.
    var aBull:Bullet1 = e.target as Bullet1;

    aBull.y -= 20;

    if (aBull.y <= 100)
    {
        aBull.removeEventListener(Event.ENTER_FRAME, BulletUpdate);
        stage.removeChild(aBull);
    }
}

P.S. Don't be shy to format your code thoroughly. It is not that much trouble as it might seem and soon you'll take a habit of it, while formatted code is clean and readable and some mistakes can be avoided just by doing the formatting properly.

Organis
  • 7,243
  • 2
  • 12
  • 14
  • Well, thank you very much sir! I'll just take a shower right now and try your solution to my problem. About the code formatting, I try to keep everything in blocks so that I can easily read what section is about what, I think it's a lot easier for my eyes and for organization, but I didn't know that declaring functions inside functions was bad, so I'll keep that in mind for now on too. Also, would you mind telling me what the "e.target as Bullet1;" do? I've never used the "as" command, so... – Wobbufest Dec 03 '17 at 22:31
  • @Wobbufest It's a type casting. Basically, you explain *"this reference is of **Bullet1** class, not just plain **Object** as you might think"* to compiler. It is not always necessary (in your code it is fine to declare **aBull** as **DisplayObject**) but sometimes it is good for you that compiler would know better what to expect or to not expect from the object you are addressing. You can read about **as** more: https://stackoverflow.com/questions/12390347/whats-the-significance-of-as-keyword-in-this-code https://stackoverflow.com/questions/14268329/as3-cast-or-as – Organis Dec 03 '17 at 22:44
  • First of all, thanks for the **as** explanation, it really helped me out. Second, I just tried the code and I'm running into the same problem, here's my BulletUpdate Event: ' function BulletUpdate(Event) { var ThisBullet:Bullet1 = Event.currentTarget as Bullet1; ThisBullet.y -= 20; if(ThisBullet.y <= 100) { ThisBullet.removeEventListener(Event.ENTER_FRAME, BulletUpdate); stage.removeChild(ThisBullet); }; };' Am I still missing something? PS.: e.target as Bullet1 didn't seem to work so I changed to Event.target – Wobbufest Dec 03 '17 at 23:27
  • Also, I separated SpawnBullet() and BulletUpdate() in two separate functions, as you pointed out was the best thing to do. – Wobbufest Dec 03 '17 at 23:28
  • @Wobbufest From the function body you provided I see that didn't copy my code, and it seems you didn't read my explanations, that's why you are still having the same error which is the result of the same erroneous script of yours. – Organis Dec 03 '17 at 23:56
  • I just got what you meant in the first question, that's what I get for missing the entire point. Nonetheless, just copied your code and while i don't get the error anymore (because the entire problem was on the argument of the function that I didn't get, so now it's solved) the bullet isn't getting removed from the stage when it's position is less than 100 on the y-axis... – Wobbufest Dec 04 '17 at 00:04
  • Ok, nevermind, I just solved it. The problem was on the "if(bullet.y <= 100)" for some reason when I changed it to if(Bullet.y <= Player.y - 400) it is deleting everything the right way. Thanks for the help with code formatting and sorry if I'm slow sometimes, I have some difficulty understanding stuff sometimes that's why I didn't like to code, but I'm working on it. Thanks a lot man! – Wobbufest Dec 04 '17 at 00:29