0

I'm writing a little flash game, and i have faced a problem adding MovieClips to stage.

I have function createBricks(), which is adding MovieClips to stage. If i do it like this:

public function createBricks():void
    {
        for (var i:int = 0; i < amountOfBricks; i++)
        {
            var brick:MovieClip = new brick2();
            brick.x = i * brick.width + 10;
            brick.y = 6;
            addChild(brick);
        }
    } 

Everything is okay, i can see bricks on stage. But the problem is I need to have some logic in Brick class.

So if I do it like this:

public function createBricks():void
    {
        for (var i:int = 0; i < amountOfBricks; i++)
        {
            var brick:MovieClip = new Brick(this);
            brick.x = i * brick.width + 10;
            brick.y = 6;
            addChild(brick);
        }
    }

I can't see MovieClips on stage. Any help is appreciated.

class Brick code:

package  
{
import flash.display.MovieClip;
import flash.events.Event;
import Arkanoid;

public class Brick extends MovieClip
{
    private var mParent:MovieClip;

    public function Brick(parent:Arkanoid) 
    {
        this.mParent = parent;
        super();
        addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }

    private function enterFrameHandler(event:Event):void
    {
        if (this.hitTestObject(mParent.view.ball))
        {
            mParent.ballYSpeed *= -1;
            mParent.removeChild(this);
            removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
    }

}

}

game class:

    package  
    {
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.MouseEvent
    import flash.events.Event;
    import Brick;

    public class Arkanoid extends MovieClip
    {
        private var mView:MovieClip;
        private var paddle:MovieClip;
        private var ball:MovieClip;
        private var amountOfBricks:int = 6;
        private var ballXSpeed:int = 10;
        private var ballYSpeed:int = 10;

        public function Arkanoid() 
        { 
            mView = new GameView();
            paddle = mView.paddle;
            ball = mView.ball;
            super();
        }

        public function start():void
        {
            addListeners();
            paddle.x = 100;
            createBricks();
            addChild(mView);
        }

        public function addListeners():void
        {
            mView.addEventListener(MouseEvent.MOUSE_MOVE, paddle_mouseMoveHandler);
            mView.addEventListener(Event.ENTER_FRAME, ball_enterFrameHandler);
        }

        private function paddle_mouseMoveHandler(event:MouseEvent):void
        {
            paddle.x = mouseX - paddle.width / 2;

            if (mouseX < paddle.width / 2)
            {
                paddle.x = 0;
            }

            if (mouseX > mView.width - paddle.width / 2)
            {
                paddle.x = mView.width - paddle.width;
            }
        }

        private function ball_enterFrameHandler(event:Event):void
        {
            ball.x += ballXSpeed;
            ball.y += ballYSpeed;

            if (ball.x >= mView.width - ball.width)
            {
                ballXSpeed *= -1;
            }

            if (ball.x <= 0)
            {
                ballXSpeed *= -1;
            }

            if (ball.y >= mView.height - ball.height)
            {
                ballYSpeed *= -1;
            }

            if (ball.y <= 0)
            {
                ballYSpeed *= -1;
            }

            if (ball.hitTestObject(paddle))
            {
                calculateBallAngle();
            }
        }

        private function calculateBallAngle():void
        {
            var ballPosition:Number = ball.x - paddle.x;
            var hitPercent:Number = (ballPosition / (paddle.width - ball.width)) - .5;
            ballXSpeed = hitPercent * 10;
            ballYSpeed = - 1;
        }

        public function createBricks():void
        {
            for (var i:int = 0; i < amountOfBricks; i++)
            {
                //var brick:MovieClip = new brick2();
                var brick:MovieClip = new Brick(this);
                brick.x = i * brick.width + 10;
                brick.y = 6;
                addChild(brick);
            }
        }

        public function get view():MovieClip
        {
            return mView;
        }
    }

}
Andrew
  • 1
  • 1
  • please edit your question to include the `Brick` class code. What value have you put in the Actionscript Linkage? It needs to match the class name, so if it's `brick2` that would explain your problem (It needs to be `Brick`), if you click the edit (pencil icon) button, does your `Brick.as` file come up? If not, the your .as file isn't in the location Flash is expecting it. – BadFeelingAboutThis Jun 23 '15 at 03:20
  • See my answer here: http://stackoverflow.com/a/27045707/1457439 – BadFeelingAboutThis Jun 23 '15 at 03:45
  • Actionscript linkage matching class Brick, if i click the edit, Brick.as is opened – Andrew Jun 23 '15 at 13:24
  • Are you sure your hit test in the enterframe handler of `Brick` isn't succeeding right away? put a trace or break point in and find out. That seems the most likely cause here. Share your .fla if that's not the problem and I can take a look – BadFeelingAboutThis Jun 23 '15 at 16:19

1 Answers1

0

1| Create class called Brick

class Brick extends MovieClip {
    var ground;
    public function Brick(_ground){
         ground = _ground;

         var brickSample:MovieClip = new brick2();
         addChild(brickSample);
    }
}

2| Now you can create your movieclips like you want

public function createBricks():void
{
    for (var i:int = 0; i < amountOfBricks; i++)
    {
       var brick:MovieClip = new Brick(this);
       brick.x = i * brick.width + 10;
       brick.y = 6
       addChild(brick);
    }
}

hope it helps

Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • Out of interest, why not define the local brick variable as a `Brick` type, instead of `MovieClip`? This will work either way, but I'm curios. –  Jun 23 '15 at 06:39
  • you are right, it will both work but in this way it is more explainable to show it is a movieclip – Özgür Ersil Jun 23 '15 at 06:43