Hey everyone, I’m converting from AS2 to AS3 and have a sore head to prove it! In a previous project I had a simple game which positions a movieclip (FlyingBox) randomly on the stage 10 times, animates it with onEnterFrame at random speeds, and then another movieclip (mcBoxExploder) can be dragged around to “pop” the mcFlyingBox movieclips, using hitTest and unLoadMovie.
I could do most of it in AS3 (the event handling, addChild, removeChild) except for the part where I attachMovie using an initialization object. In AS2, the initialization object gets a property for each new instance of FlyingBox’s velocity and is also used for the hitTest event handling.
How do I do this in AS3? Please help!
Here is the AS2 code: (my library contains three symbols: FlyingBox, BoxExploder and NewGame (button))
/*** declare variables ********************/
var boxCount:Number = 0;
var boxesPerGame:Number = 10;
/*** create objects ***********************/
var init:Object = new Object();
/*** define functions ********************/
function createFlyingBox(thisName:String):Void
{
var thisX:Number = Math.round(Math.random() * 600) + 50;
var thisY:Number = Math.round(Math.random() * 400) + 50;
init.xVelocity = Math.round(Math.random() * 20) - 10;
init.yVelocity = Math.round(Math.random() * 20) - 10;
var nextDepth:Number = this.getNextHighestDepth();
this.attachMovie("FlyingBox", thisName, nextDepth, init);
this[thisName]._x = thisX;
this[thisName]._y = thisY;
}
/*** handle events *************************/
init.onEnterFrame = function():Void
{
this._x += this.xVelocity;
this._y += this.yVelocity;
if(this._x <= 0 || this._x >= 700){this.xVelocity *= -1;}
if(this._y <= 0 || this._y >= 500){this.yVelocity *= -1;}
if(this.hitTest(mcBoxExploder)){
this.unloadMovie();
}
}
mcBoxExploder.onPress = function():Void {this.startDrag(); }
mcBoxExploder.onRelease = function():Void {this.stopDrag(); }
btnNewGame.onRelease = function():Void
{
for(var i:Number = 0; i < boxesPerGame; i++)
{
var instanceName:String = "mcFlyingBox" + boxCount;
createFlyingBox(instanceName);
boxCount++;
}
}
This is how far I got in AS3:
/**** declare variables ******************/
var boxCount:Number = 1;
var boxesPerGame:Number = 10;
/**** create objects **********************/
/**** define functions ********************/
function createFlyingBox(thisName:String):void {
var t:FlyingBox = new FlyingBox();
t.name=thisName;
this.addChild(t);
var thisX:Number = Math.round(Math.random() * 600) + 50;
var thisY:Number = Math.round(Math.random() * 400) + 50;
t.x = thisX;
t.y = thisY;
}
/**** handle events ***********************/
function clickHandler(evt:MouseEvent):void {
var instanceName:String;
for (var i:Number = 0; i < boxesPerGame; i++) {
instanceName = "mcFlyingBox" + boxCount;
createFlyingBox(instanceName);
boxCount++;
}
}
btnNewGame.addEventListener(MouseEvent.CLICK, clickHandler);
mcBoxExploder.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
function mouseDown(event:MouseEvent):void {
mcBoxExploder.startDrag();
}
mcBoxExploder.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
function mouseUp(event:MouseEvent):void {
mcBoxExploder.stopDrag();
/* How do I test for hitting each separate movieclip?
if (mcBoxExploder.hitTestObject(mcFlyingBox)) {
trace("hitting");
} else {
trace("not hitting");
}*/
}
mcBoxExploder.buttonMode = true;//show hand cursor