samedi 10 décembre 2016

How to check whether a child exists in class code?

Trying this the second time. Hopefully I won't get a lot of backlash this time.

I've attempted to make a weapon instance be spawned by player ship instance through the ship's Class code. My game has a lot of things tied into this and as expected, I didn't find anything so specific attempted anywhere. Essentially what my code does right now is spawn a weapon instance and has it move along with the ship because it's parented to it.

That's all great until I change ship type. Changing ship type changes a variable defined in the main game code (which you can also see called out in "if" statements in the code below) and changes the frames through which the game loops through. The ship has 6 frames, 2 for each ship type to make it look as if it has a dynamic animation and such.

Essentially, the weapon needs to have a different x and y coordinate relative to the ship when ship type is changed so that the weapon is visible where appropriate. Problem is though that I cannot change the weapon by turning the addWeapon function into an ENTER_FRAME function because otherwise it will just keep spawning multiple weapons every frame on top of each other. I could avoid this if I also made an "if" statement check for whether the weapon exists as a child of the player ship, but I have no idea how to do that; trying what others have done, with something along the lines of "if (!this.contains(weaponAttached))" gives me error #1119.

In short, I don't know how to check whether a child exists for the playerShip so that the game stops spawning more of them once one is created if I were to turn the addWeapon into an ENTER_FRAME function. I've also tried to make it so that the addWeapon simply calls out another ENTER_FRAME function, which would define the x and y coordinates of the weapon based on which type of ship is selected (remember that you can change ship type in-game before the first level and in-between levels), but the weaponAttached is a weapon instance variable which can only be used for that one addWeapon function and not outside of it, so this isn't a viable method.

Here is the Class code for playerShip:

package  {

    import flash.display.MovieClip;

    public class playerShipCode extends MovieClip {
        private var _root: Object;

        public function playerShipCode() {
            // constructor code
            addEventListener(Event.ADDED, beginClass);
            addEventListener(Event.ENTER_FRAME, entFrame);
        private function beginClass(event:Event):void
            _root = MovieClip(root);
        private function addWeapon():void
            var weaponAttached = new weaponCode();
            weaponAttached.scaleX = 1;
            weaponAttached.scaleY = 1;
            weaponAttached.x = this.x + 270;
            weaponAttached.y = this.y - 110;
            this.addChildAt(weaponAttached, 0);
        private function entFrame(event:Event):void
            if (_root.shipSelected == 1)
                _root.playerXSpeed = 6;
                _root.playerYSpeed = 4;
                this.scaleX = .5;
                this.scaleY = .5;
                if (this.currentFrame == 3)
            else if (_root.shipSelected == 2)
                _root.playerXSpeed = 4;
                _root.playerYSpeed = 2;
                this.scaleX = .4;
                this.scaleY = .4;
                if (this.currentFrame == 1 || this.currentFrame == 2 || this.currentFrame == 5)
            else if (_root.shipSelected == 3)
                _root.playerXSpeed = 8;
                _root.playerYSpeed = 6;
                this.scaleX = .3;
                this.scaleY = .3;
                if (this.currentFrame == 1 || this.currentFrame == 2 || this.currentFrame == 3 || this.currentFrame == 4)


Aucun commentaire:

Enregistrer un commentaire