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;
}
}
}