Hey
Im currently making a platform engine and i have built the player with a scrolling background. The player is on the stage. I have a enemy named redsquare and the enemy is contained in the background movieclip so that when the background scrolls the enemy scrolls too.
The Problem:
The big problem is that when the player hits the redsquare i get the following error
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
here is the document class
package {
import flash.display.*;
import flash.events.*;
import flash.ui.*;
public class Zebra2 extends MovieClip {
var vx:int;
var vy:int;
var rightInnerBoundry:uint;
var leftInnerBoundry:uint;
var topInnerBoundry:uint;
var bottomInnerBoundry:uint;
var redsquare:Red;
public function Zebra2() {
init();
}
public function init():void {
vy = 0;
vx = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, Keypress);
stage.addEventListener(KeyboardEvent.KEY_UP, Keyup);
stage.addEventListener(Event.ENTER_FRAME, PlayerMove);
rightInnerBoundry = (stage.stageWidth /2) + (stage.stageWidth /16);
leftInnerBoundry = (stage.stageWidth /2) - (stage.stageWidth /16);
topInnerBoundry = (stage.stageHeight /2) - (stage.stageHeight /16);
bottomInnerBoundry = (stage.stageHeight /2) + (stage.stageHeight /16);
redsquare = new Red();
background.addChild(redsquare);
redsquare.x = 400;
redsquare.y = 100;
}
public function Keypress(event:KeyboardEvent):void {
if (event.keyCode == 37) {
vx = -5;
} else if (event.keyCode == 39) {
vx = 5;
} else if (event.keyCode == 40) {
vy = 5;
} else if (event.keyCode == 38) {
vy = -5;
}
}
public function Keyup(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 39) {
vx =0;
} else if (event.keyCode == 40 || event.keyCode == 38) {
vy =0;
}
}
public function PlayerMove(event:Event):void {
var playerHalfWidth:uint = player.width / 2;
var playerHalfHeight:uint = player.height / 2;
var backgroundHalfWidth:uint = background.width / 2;
var backgroundHalfHeight:uint = background.height / 2;
if (player.x - playerHalfWidth < leftInnerBoundry) {
player.x = leftInnerBoundry + playerHalfWidth;
rightInnerBoundry = (stage.stageWidth /2) + (stage.stageWidth / 16);
background.x -= vx;
} else if (player.x + playerHalfWidth > rightInnerBoundry) {
player.x = rightInnerBoundry - playerHalfWidth;
leftInnerBoundry = (stage.stageWidth /2) - (stage.stageWidth / 16);
background.x -= vx;
}
if (player.y - playerHalfHeight < topInnerBoundry) {
player.y = topInnerBoundry + playerHalfHeight;
bottomInnerBoundry = (stage.stageHeight /2) + (stage.stageHeight / 16);
background.y -= vy;
} else if (player.y + playerHalfHeight > bottomInnerBoundry) {
player.y = bottomInnerBoundry - playerHalfHeight;
topInnerBoundry = (stage.stageHeight /2) - (stage.stageHeight/ 16);
background.y -= vy;
}
if (background.x - backgroundHalfWidth > 0) {
background.x = 0 + backgroundHalfWidth;
leftInnerBoundry = 0;
} else if (background.x + backgroundHalfWidth < stage.stageWidth) {
background.x = stage.stageWidth - backgroundHalfWidth;
rightInnerBoundry = stage.stageWidth;
}
if (background.y + backgroundHalfHeight < stage.stageHeight) {
background.y = stage.stageHeight - backgroundHalfHeight;
bottomInnerBoundry = stage.stageHeight;
} else if (background.y - backgroundHalfHeight > 0) {
background.y = 0 + backgroundHalfHeight;
topInnerBoundry = 0;
}
player.x += vx;
player.y += vy;
if (player.hitTestObject(redsquare)) {
background.removeChild(redsquare);
}
}
}
}