hitTestObject issue // remove stage Error #2025 - actionscript-3

hey :S I`m trying to make 2 things so far but neither work :(
The 1st is to make an Row of 5 Ships that position them selves from the top to left.Then add an Event to make them move down.
But every time i try to make a new Row of 5 new ships ... they position themselves after the last ship from the previous Row.I did think of a way to fix that by waiting the 1st Row of ships to go beyond the stage and then remove them from the stage and splice from the Array but ... when I increase the speed of spawning (when i have 2 rows with 5 ships on the stage or more)that does't work.
And the second error I think is from that fact .... that I have 2 rows at the same time on the stage.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MainClass/doShips()
at MainClass/everyFrame()
I dont know what exactly to paste from the code so i had to push everything in the MainClass of the FLA.fail :D but i did it neatly! so if anyone helped me he would track the code easily
so its a simple FLA.fail with 4 MC and in those 4 MC there is no code
a Bullet
a Ship with colour Yellow
a Ship2 with colour Green so i can see the randomising
and a Player with the class Turret
so the MainClass of the FLA is :
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class MainClass extends MovieClip {
protected var mouseSpeed:Number = 20;
protected var _thePlayer:Turret = new Turret();
protected var shipCount:Number;
protected var shipCount2:Number;
protected var shipArray:Array = new Array();
protected var counter:int = 0;
//variables for bullets
private var shootCooldown:Number = 0; //speed of the fire
private var firing:Boolean = false;//bullets triger
private var numToShoot:int = 1; //bullets
const MAX_COOLDOWN = 10; //bullets timer
protected var _bulletsArray:Array = new Array();
public function MainClass() {
_thePlayer.x = stage.stageWidth/2
_thePlayer.y = stage.stageHeight - _thePlayer.height/2
addChild(_thePlayer)
addEventListener(Event.ENTER_FRAME, everyFrame)
stage.addEventListener(MouseEvent.MOUSE_DOWN, startFire);
stage.addEventListener(MouseEvent.MOUSE_UP, stopFire);
}
private function startFire(ev:MouseEvent) {
firing = true;
}
private function stopFire(ev:MouseEvent) {
firing = false;
}
//every frame do this
protected function everyFrame(ev:Event):void{
//Moving The Turret
moveTheTurret();
//increase the counter every frame
counter++;
if(counter % 70 == 0){
//position the ships on the stage
positionShips();
}
//handle the ships when they are added on the stage
doShips();
//when to fire
updateFire();
//handle the bullets
doBullets();
}
//Moving The Turret
protected function moveTheTurret() {
if (mouseX > _thePlayer.x +15) {
_thePlayer.x += mouseSpeed;
} else if (mouseX < _thePlayer.x - 15) {
_thePlayer.x -= mouseSpeed;
} else {
_thePlayer.x = mouseX;
}
}
//createShips and position them
protected function positionShips() {
shipCount = 3;
shipCount2 = 2;
var gap = 10;
for (var i:int = 0; i < shipCount; i++) {
var s = new Ship();
shipArray.push(s);
}
for (var j:int = 0; j < shipCount2; j++) {
s = new Ship2();
shipArray.push(s);
}
var array:Array=new Array();
while (shipArray.length>0) {
var index:uint = Math.floor(Math.random() * shipArray.length);
array.push(shipArray[index]);
shipArray.splice(index,1);
}
shipArray = array;
//shipsArray has been randomized
for (var k:int = shipArray.length - 1; k >= 0; k--) {
addChild(shipArray[k]);
shipArray[k].x = shipArray[k].width/2 + (shipArray[k].width * k) + (gap*k);
}
}
//move the ships
protected function doShips() {
for (var i:int = shipArray.length - 1; i >= 0; i--) {
shipArray[i].y +=3 //make the Ships fall down
for (var bcount= _bulletsArray.length-1; bcount >= 0; bcount--) {
//if the bullet is touching the ship
if (shipArray[i].hitTestObject(_bulletsArray[bcount])) {
//if we get here it means there`s is a collision
removeChild(_bulletsArray[bcount]);
_bulletsArray.splice(bcount,1);
removeChild(shipArray[i]);
_bulletsArray.splice(i,1);
}
}
//if it gets over 380 remove from stage and splice from the array
if(shipArray[i].y > 380){ // stage.stageHeight)
removeChild(shipArray[i]);
shipArray.splice(i,1);
}
}
}
//when to fire
public function updateFire() {
//if we are currently holding the mouse down
if(firing == true){
fire();
}
//reduce the cooldown by 1 every frame
shootCooldown--;
}
//Shoot bullets
private function fire() {
if (shootCooldown <= 0) {
//reset the cooldown
shootCooldown = MAX_COOLDOWN
for (var i=0; i<numToShoot; i++){
//spown a bullet
var b = new Bullet();
//set the rotation of the bullet
b.rotation = -90
b.x = _thePlayer.x;
b.y = _thePlayer.y;
//add the bullet to the list/Array of _bulletsArray
_bulletsArray.push(b);
//add the bullet to the perent object;
addChild(b);
}
}
}
//handling the bullets
protected function doBullets(){
//make a for loop to iterate all the _bulletsArray on the screen
for (var bcount:int = _bulletsArray.length-1; bcount>=0; bcount--) {
//make the bullets move Up
_bulletsArray[bcount].y -=20;
//if the bullet is beyond the screen remove from the stage and the Array
if(_bulletsArray[bcount].y < 0){
removeChild(_bulletsArray[bcount])
_bulletsArray.splice(bcount,1);
}
}
}
}
}
I dont know how to fix neither of the 2 problems.Would be very thankful for any help!!!
Thanks in advance.

Related

Some of the collisions are not responding

im developing a game; its like a space craft going horizontally and enemy crafts coming oppositely..Enemies are destroyed only when it collides with the trail of the craft.
Here is the main class:
public function Velocity() {
//int static var
trailspeed = 10;
shipspeed = 5;
//init ship//
ship = new Ship();
ship.x = 337;
ship.y = 216;
//int arrays
enemies = new Array();
//init starting state
score = 0;
health = 100;
//Events
addEventListener(Event.ENTER_FRAME, motion);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
//init container and trail
_cont = new MovieClip();
//addChild
addChild(_cont);
addChild(ship);
timerset();
}
public function motion(e:Event){
//adding the trails
trail = new Trail();
trail.x = ship.x;
trail.y = ship.y;
_cont.addChild(trail);
checkcollision();
}
public function checkcollision(){
for(var _e:int = enemies.length - 1; _e >=0; _e--){
for(var _t:int = _cont.numChildren - 1; _t >=0; _t--){
if(enemies[_e].hitTestObject(_cont.getChildAt(_t))){
enemies[_e].remove();
return;
}
}
}
}
public function removeenemies(_e:Enemy){
for(var i in enemies){
if(enemies[i] == _e){
enemies.splice(_e);
}
}
}
and here is the Enemy class:
public class Enemy extends MovieClip {
private var _speed:Number;
public function Enemy(ypos, speed) {
this.x = 550;
this.y = ypos;
_speed = speed;
addEventListener(Event.ENTER_FRAME, motion);
}
public function motion(e:Event){
this.x -= _speed;
checkoffscreen();
}
public function remove(){
MovieClip(parent).removeenemies(this);
MovieClip(parent).removeChild(this);
removeEventListener(Event.ENTER_FRAME, motion);
return;
}
public function checkoffscreen(){
if(this.x < 0){
MovieClip(parent).removeenemies(this);
MovieClip(parent).removeChild(this);
removeEventListener(Event.ENTER_FRAME, motion);
}
}
}
}
The problem here is after 2 or 3 successful collision detections one goes undetected.. and hence the chances is like 2/3 ...how can i program a 100% successful collision???pls help
I want to recommend not to delete objects from within themselves, instead have a simple isDead boolean inside the the Enemy and in the main class when you loop through enemies check if isDead is set to true, if so then remove the object from the array and delete it from the Stage; no need for removeenemies().
Back to the real problem you're having. You're doing a good job iterating through the list of enemies in reverse since you do expect to remove some objects, that of course results in shift of entries in the array.
It's hard to say what's wrong with this code. I'd say lose removeenemies(). Move any collision testing inside the Enemy and in the Main keep everything in a nice linear update function like so:
public function update() {
for(var i:int = enemies.length - 1; i >=0; i--){
var enemy:Enemy = enemies[i];
enemy.checkCollision(); // Can set isDead = true;
if(enemy.isDead) {
enemies.splice(i);
removeChild(enemy);
}
}
}
Finally I found out the answer.
the problem was with the removeenemies function
replacing splice(_e) wtih splice(_e, 1) solved the problem

Action Script 3: Adding an gotoAndStop Animation

So the other day I found a tutorial on how to create a pattern lock screen in action script. To do so I had to create a class, I have a good grasp on how the class is working. But I want to add an animation so when the user goes over the dots in the pattern and animation plays. But I have no idea how to do something like this through the class. Here is the code I used in my class.
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
public class Main extends Sprite
{
private var dots:Array = []; // Stores the in stage movieclips
private var pattern:Array = []; //The pattern entered by the user
private var pass:Array = [1,4,7,8,5,2,5]; //The correct pattern to proceed
public function Main():void
{
dots = [one,two,three,four,five,six,seven,eight,nine]; //add the clips in stage
addListeners();
}
private function addListeners():void //adds the listeners to each dot
{
var dotsLength:int = dots.length;
for (var i:int = 0; i < dotsLength; i++)
{
dots[i].addEventListener(MouseEvent.MOUSE_DOWN, initiatePattern);
dots[i].addEventListener(MouseEvent.MOUSE_UP, stopPattern);
}
}
/* Adds a mouse over listener and uses it to add the number of the dot to the pattern */
private function initiatePattern(e:MouseEvent):void
{
var dotsLength:int = dots.length;
for (var i:int = 0; i < dotsLength; i++)
{
dots[i].addEventListener(MouseEvent.MOUSE_OVER, addPattern);
}
pattern.push(dots.indexOf(e.target) + 1); //adds the array index number of the clip plus one, because arrays are 0 based
}
private function addPattern(e:MouseEvent):void
{
pattern.push(dots.indexOf(e.target) + 1); //adds the pattern on mouse over
}
private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
{
var dotsLength:int = dots.length;
for (var i:int = 0; i < dotsLength; i++)
{
dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
}
checkPattern();
}
private function checkPattern():void //compares the patterns
{
var pLength:int = pass.length;
var correct:int = 0;
for (var i:int = 0; i < pLength; i++) //compares each number entered in the user array to the pass array
{
if (pass[i] == pattern[i])
{
correct++;
}
}
if (correct == pLength) //if the arrays match
{
//Hides Sign In
MovieClip(root).LockScreen.visible = false;
MovieClip(root).RTID.visible = false;
MovieClip(root).SignIn.visible = false;
//Turns On Main Menu
MovieClip(root).gamemenu_mc.visible = true;
MovieClip(root).biggamesmenu_mc.visible = true;
MovieClip(root).totaltextmenu_mc.visible = true;
MovieClip(root).tmenu_mc.visible = true;
MovieClip(root).smenu_mc.visible = true;
MovieClip(root).optionsmenu_mc.visible = true;
}
pattern = []; //clears the user array
}
}
}
Easiest way I can think to do this is:
inside your dot movie clip, put a stop() action on the first frame
create your animation on the dot timeline and on the last frame of the animation put another stop().
In your mouse over function, tell the dot to play.
private function addPattern(e:MouseEvent):void
{
var dot:MovieClip = MovieClip(e.currentTarget);
if(dot.currentFrame < 2) dot.play(); //play only if on the first frame
pattern.push(dots.indexOf(dot) + 1); //adds the pattern on mouse over
}
Reset the dots animation
private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
{
for (var i:int = 0; i < dots.length; i++)
{
dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
dots[i].gotoAndStop(1); //go back to the first frame
}
checkPattern();
}
Alternatively, if you're just want something simple like a layer of the dot fading in/out or the size of the dot increasing, you could just use a tweening library and tween the appropriate property on mouse over.
If you wanted to draw lines to connect the dots, you could do this:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.Shape;
public class Main extends Sprite
{
private var lineContainer:Shape = new Shape();
private var dots:Array = []; // Stores the in stage movieclips
private var pattern:Array = []; //The pattern entered by the user //don't make life hard, just store the object itself instead of the index
private var pass:Array;
public function Main():void
{
dots = [one,two,three,four,five,six,seven,eight,nine]; //add the clips in stage
pass = [one,four,seven,eight,five,two,five]; //The correct pattern to proceed
addChildAt(lineContainer, this.getChildIndex(one)); //the line container right behind the first dot.
addListeners();
}
private function addListeners():void //adds the listeners to each dot
{
var dotsLength:int = dots.length;
for (var i:int = 0; i < dotsLength; i++)
{
dots[i].addEventListener(MouseEvent.MOUSE_DOWN, initiatePattern);
dots[i].addEventListener(MouseEvent.MOUSE_UP, stopPattern); //you could attach this to `this` instead of each dot, same result
}
}
/* Adds a mouse over listener and uses it to add the number of the dot to the pattern */
private function initiatePattern(e:MouseEvent):void
{
pattern = []; //reset array
lineContainer.graphics.clear(); //clear lines
for (var i:int = 0; i < dots.length; i++)
{
dots[i].addEventListener(MouseEvent.MOUSE_OVER, addPattern);
}
addPattern(e); //trigger the mouse over for this element
}
private function addPattern(e:MouseEvent):void
{
//if (pattern.indexOf(e.currentTarget) == -1) { //wrap in this if statemnt if only wanted a dot to be selected once (like Android)
pattern.push(e.currentTarget); //adds the pattern on mouse over
drawLines();
var dot:MovieClip = MovieClip(e.currentTarget);
if(dot.currentFrame < 2) dot.play(); //play only if on the first frame
//}
}
private function drawLines():void {
lineContainer.graphics.clear(); //clear the current lines
lineContainer.graphics.lineStyle(5, 0xFF0000); //thickness (5px) and color (red) of the lines
if (pattern.length > 1) { //don't draw if there aren't at least two dots in the pattern
lineContainer.graphics.moveTo(pattern[0].x + pattern[0].width * .5, pattern[0].y + pattern[0].height * .5); //move to first
for (var i:int = 1; i < pattern.length; i++) {
lineContainer.graphics.lineTo(pattern[i].x + pattern[i].width * .5, pattern[i].y + pattern[i].height * .5); //draw a line to the current dot
}
}
lineContainer.graphics.endFill();
}
private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
{
for (var i:int = 0; i < dots.length; i++)
{
dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
dots[i].gotoAndStop(1); //go back to the first frame
}
checkPattern();
}
private function checkPattern():void //compares the patterns
{
var pLength:int = pass.length;
var correct:int = 0;
for (var i:int = 0; i < pLength; i++) //compares each number entered in the user array to the pass array
{
if (pass[i] == pattern[i])
{
correct++;
}
}
if (correct == pLength) //if the arrays match
{
//Hides Sign In
MovieClip(root).LockScreen.visible = false;
MovieClip(root).RTID.visible = false;
MovieClip(root).SignIn.visible = false;
//Turns On Main Menu
MovieClip(root).gamemenu_mc.visible = true;
MovieClip(root).biggamesmenu_mc.visible = true;
MovieClip(root).totaltextmenu_mc.visible = true;
MovieClip(root).tmenu_mc.visible = true;
MovieClip(root).smenu_mc.visible = true;
MovieClip(root).optionsmenu_mc.visible = true;
}
pattern = []; //clears the user array
lineContainer.graphics.clear(); //clear the lines
}
}
}

AS3 - Remove a Specific Instance of a MovieClip with removeChild() Function

I'm working on a Flash game for an assignment. It's a pretty standard missile defense-type game, with a rotating missile launcher in the center firing up at passing bombers above. The missiles and bombers are functioning correctly by themselves, but I'm running into a problem when I try to get the two to interact. Specifically, when a missile hits a bomber, I want the specific instances of that missile and that bomber to be removed from the screen and have their respective event listeners removed, but everything I've tried has failed and I can't seem to figure out just how to do it.
Here are the Main, Bomber, and Missile classes I'm working with:
The Main Class:
package {
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Main extends MovieClip {
public var background:Background;
public var launcher:Launcher;
public var mount:Mount;
public var missile:Missile;
public var salvo:Array = [];
public var bomber:Bomber;
public var squadron:Array = [];
/*
* This function sets up the permanent items (items that remain on-stage for
* the duration of the game) and adds event listeners that call functions to
* add non-permanent items to the stage
*/
public function Main() {
// Add background to the stage
background = new Background(stage);
stage.addChild(background);
stage.setChildIndex(background, 0);
// Add the rotating launcher to the stage
launcher = new Launcher(stage);
stage.addChild(launcher);
stage.setChildIndex(launcher, 1);
// Add the static mount to the stage (on top of launcher)
mount = new Mount(stage);
stage.addChild(mount);
stage.setChildIndex(mount, 2);
// Call loop() every new frame
stage.addEventListener(Event.ENTER_FRAME, loop);
// Call fire() every time the mouse is clicked
stage.addEventListener(MouseEvent.CLICK, fire);
}
/*
* This function runs every time the program enters a new frame, or 60 times
* every second. Each time this function runs, it tries to add a new Bomber
* to the squadron array and checks to see if there are any Missiles or
* Bombers currently in their respective arrays (and if so, calls a function
* to make them move).
*/
public function loop(evt:Event) {
// If the random number generated by Math.random() is less than
// waveLimiter, create a new Bomber and add it to the squadron array.
if(Math.random() < 0.02 /* Change this number to change how fast bombers spawn */) {
bomber = new Bomber(stage);
bomber.addEventListener(Event.REMOVED_FROM_STAGE, removeBomber); // If the Bomber is removed from the stage, call removeBomber() to remove its event handler.
squadron.push(bomber);
stage.addChild(bomber);
stage.setChildIndex(bomber, 1);
}
// Check to see if there is at least one missile in the salvo array, and
// if so, call Missile.velocity() to make it move.
if(salvo.length > 0) {
for(var i:int = salvo.length - 1; i >= 0; i--) {
salvo[i].velocity();
}
}
// Check to see if there is at least one bomber in the squadron array,
// and if so, call Bomber.fly() to make it move.
if(squadron.length > 0) {
for(var j:int = squadron.length - 1; j >= 0; j--) {
squadron[j].fly();
}
}
}
/*
* This function checks for a mouse click, and if it detects one, creates a
* new Missile and adds it to the salvo array.
*/
public function fire(evt:MouseEvent) {
missile = new Missile(stage, launcher.rotation);
missile.addEventListener(Event.REMOVED_FROM_STAGE, removeMissile); // If the Missile is removed from the stage, call removeMissile() to remove its event handler.
salvo.push(missile);
stage.addChild(missile);
stage.setChildIndex(missile, 1);
}
/*
* This function removes the EVENT LISTENER for the current Missile instance.
* It does not remove the Missile itself from the stage.
*/
public function removeMissile(evt:Event):void {
evt.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removeMissile);
salvo.splice(salvo.indexOf(evt.currentTarget), 1);
}
/*
* This function removes the EVENT LISTENER for the current Bomber instance.
* It does not remove the Bomber itself from the stage.
*/
public function removeBomber(evt:Event) {
evt.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removeBomber);
squadron.splice(squadron.indexOf(evt.currentTarget), 1);
}
}
}
The Bomber class:
package {
import flash.display.Stage;
import flash.display.MovieClip;
public class Bomber extends MovieClip {
var stageInstance:Stage;
var randomNumber:Number = Math.round(Math.random() * 1);
public function Bomber(stageInstance:Stage):void {
this.stageInstance = stageInstance;
if(randomNumber == 1) {
x = -39;
y = (Math.random() * 120) + 30;
}
else if(randomNumber == 0) {
scaleX *= -1;
x = 679;
y = (Math.random() * 120) + 30;
}
}
public function fly():void {
if(randomNumber == 1) {
x = x + 4;
}
else if(randomNumber == 0) {
x = x - 4;
}
if(x > 680 || x < -40) {
this.parent.removeChild(this);
}
}
}
}
The Missile Class:
package {
import flash.display.Stage;
import flash.display.MovieClip;
public class Missile extends MovieClip {
var stageInstance:Stage;
var velocityX:Number;
var velocityY:Number;
var speed:Number = 10;
var rotationRadians:Number;
var rotationDegrees:Number;
public function Missile(stageInstance:Stage, rotationDegrees:Number):void {
this.stageInstance = stageInstance;
x = 320;
y = 363;
rotation = rotationDegrees;
rotationRadians = rotationDegrees * Math.PI / 180;
}
public function velocity():void {
velocityX = Math.cos(rotationRadians) * speed;
velocityY = Math.sin(rotationRadians) * speed;
x += velocityX;
y += velocityY;
if(x > 640 || x < 0 || y > 480 || y < 0) {
this.parent.removeChild(this);
}
}
}
}
In the Main Class, I've tried adding something like this:
if(squadron.length > 0) {
for(var j:int = squadron.length - 1; j >= 0; j--) {
squadron[j].fly();
if(salvo.length > 0) {
if(missile.hitTestObject(squadron[j])) {
this.parent.removeChild(this);
}
}
}
}
But no luck. I've also tried using a trace statement, and it doesn't even give me an output, which leads me to think it's not even detecting collision at all. Any ideas?
Update: I added a few more details.
this represents the object on which the function is called. So
this.parent.removeChild(this); makes no sense when it is written in the main class.
When you write it in the Missile Class, this is the Missile instance, and this.parent is the stage.
Try replacing it with: stage.removeChild(missile), in the last sample of code you posted, and call removeMissile() just after.
Try to use stage.removeChild instead this.parent.removeChild(this);
Actually you have many problems in your code. First of all, you don't need to work with Stage. You can work with your main container Main. When you add object to the display list, don't do after setChildIndex. In your code It doesn't have any sense. Also you don't need any length conditions. And create light objects by extending Sprite, not MovieClip.
Code for your loop, for missiles:
private function loop(e: Event):void {
//...not full listing
var i:uint, j:uint, salvos:uint = salvo.length, bombers:uint = squadron.length, missile:Missle, bomber:Bomber;
var disposeMissiles:Array = [];
var disposeBombers:Array = [];
//Rendering missiles
for (i = 0; i < salvos; ++i) {
missile = salvo[i];
missile.valocity();
for (j = 0; j < bombers; ++j) {
bomber = squadron[j];
if (!bomber.isHitted() && missile.hitTestObject(bomber)) {
//Dispose both missile and bomber
bomber.setHitted = true;
disposeMissiles.push(missile);
disposeBombers.push(bomber);
}
}
}
//Clear lists and display list
disposeObjects(disposeMissiles, salvo);
disposeObjects(disposeBombers, squadron);
}
private function disposeObjects(objects:Array, from:Array):void {
//Create interface for both missiles and bombers, like IGameActor
var i:uint, len:uint = objects.length, objectToRemove:IGameActor;
for (i; i < len; ++i) {
objectToRemove = objects[i];
//Remove from the display list, in your design Parent is Stage
this.stage.removeChild(DisplayObject(objectToRemove));
//Release memory, links, event listeners
objectToRemove.dispose();
//Try manage also indexes, splice is slow operation
from.splice(from.indexOf(objectToRemove), 1);
}
}

AS3 not recognising MovieClips - 1046: Type was not found or was not a compile-time constant: MP_00

So Im new to AS3, trying to make a simple videogame for smartphones.
And it was all going smooth until I hit this problem.
I was using and manipulating objects from timeline without a problem and than all the sudden whatever I try I get the 1046.
Here is some code that gets an error:
mp = new MP_00();
And at the top I have this:
import flash.display.MovieClip;
var mp:Movieclip;
And at the end this:
function mapMove(){
mp.y = mp.y - playerSpeed;
}
Im searching for a solution all the time, but nobody seems to have the same problem.
I do have AS linkage set to MP_00 and witch ever object I try to put in, it dosent work.
While objects put in on the same way before, they work.
For example I have this
var player:MovieClip;
player = new Player();
with AS Linkage set to Player, and that works.
This is all done in Flash Professional cs6
EDIT 1
full code
Keep in mind that a lot of stuff is placeholder or just not finished code.
On this code Im getting the error twice for the same object
Scene 1 1046: Type was not found or was not a compile-time constant:
MP_00.
Scene 1, Layer 'Actions', Frame 1, Line 165 1046: Type was not
found or was not a compile-time constant: MP_00.
Thats the errors.
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.engine.SpaceJustifier;
import flashx.textLayout.operations.MoveChildrenOperation;
/*----------------------------Main VARs-----------------------------*/
var STATE_START:String="STATE_START";
var STATE_START_PLAYER:String="STATE_START_PLAYER";
var STATE_PLAY:String="STATE_PLAY";
var STATE_END:String="STATE_END";
var gameState:String;
var player:MovieClip;
var playerSpeed:Number;
var map:Array;
var bullets:Array;
//holds civil vehicles
var civil:Array;
//holds enemy vehicles
var enemy:Array;
var explosions:Array;
var BBullet:MovieClip;
//maps
var mp:MovieClip;
/*
var MP_01:MovieClip;
var MP_02:MovieClip;
var MP_03:MovieClip;
*/
//sets the bullet type and properties
var BType = "BBasic";
var BProp:Array;
//bullet preperties by type
var BBasic:Array = new Array(1, 1, 100, 50, 0, new BBasicA());
/**
ARRAY SETTING
0 = bullet position (middle , back, sides etc...)
1-middle front
2-left side front
3-right side front
4-left and right side middle
5-back
7-left and right side wheels
1 = bullet direction
1-forward
2-back
3-sides
2 = fire speed (in millisecounds so 1000 = 1sec)
3 = speed of movement
4 = damage 10-100
5 = name of the firing animation in library
6 = name of launch animation in library
7 = name of impact animation in library
**/
var level:Number;
//BCivil speed and randomness
var BCSpeed:Number = 3;
var BCRand:Number = 120;
/*------------------------------Setup-------------------------------*/
introScreen.visible = false;
loadingScreen.visible = false;
gameScreen.visible = false;
endScreen.visible = false;
//map visibility
//MpRSimple.visible = false;
/*---------------------------Intro screen--------------------------*/
/*-----------------------------mainScreen---------------------------*/
mainScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
gameStart();
}
function gameStart():void
{
//Move main screen from stage
mainScreen.visible = false;
//Begin loading the game
loadingScreen.visible = true;
gameState = STATE_START;
trace (gameState);
addEventListener(Event.ENTER_FRAME, gameLoop);
}
/*----------------------------gameLoop-----------------------------*/
function gameLoop(e:Event):void
{
switch(gameState)
{
case STATE_START:
startGame();
break;
case STATE_START_PLAYER:
startPlayer();
break;
case STATE_PLAY:
playGame();
break;
case STATE_END:
endGame();
break;
}
}
/*-_________________________-Game STATES-_________________________-*/
/*---------------------------STATE_START---------------------------*/
function startGame():void
{
level = 1; //setting level for enemies
//Graphics
//player icon
player = new Player();
//add bullet holder
bullets = new Array();
//basicCivil icon
civil = new Array();
//basicEnemy icon
enemy = new Array();
//holds explosions
explosions = new Array();
//map
//mp = new MP_00();
//var mp:MP_00 = new MP_00();
//Load map parts
//End startGame
gameState = STATE_START_PLAYER;
trace(gameState);
}
/*------------------------STATE_START_PLAYER-----------------------*/
function startPlayer():void
{
//start the player
//set possition of player
player.y = stage.stageHeight - player.height;
addChild(player);
addEventListener(Event.ENTER_FRAME, movePlayer);
//changing screens
gameScreen.visible = true;
//start game
gameState = STATE_PLAY;
trace(gameState);
}
//player controll
function movePlayer(e:Event):void
{
//gameScreen.visible = true;
//mouse\touch recognition
player.x = stage.mouseX;
player.y = stage.mouseY;
//player does not move out of the stage
if (player.x < 0)
{
player.x = 0;
}
else if (player.x > (stage.stageWidth - player.width))
{
player.x = stage.stageWidth + player.width;
}
}
//setting bullet type
if (BType == "BBasic")
{
BProp = BBasic;
/*case BMissile;
BProp = BMissile;
break; */
}
//creating bullets
//gameScreen.fire_btn.addEventListener(MouseEvent.CLICK, fireHandler());
/*
function fireHandler():void
{
var bulletTimer:Timer = new Timer (500);
bulletTimer.addEventListener(TimerEvent.TIMER, timerListener);
bulletTimer.start();
trace("nja");
}
function timerListener(e:TimerEvent):void
{
//need and if statement to determine the bullet speed and travel depended on type of bullet
var tempBullet:MovieClip = /*BProp[5] new BBasicA();
//shoots bullets in the middle
tempBullet.x = player.x +(player.width/2);
tempBullet.y = player.y;
//shooting speed
tempBullet.speed = 10;
bullets.push(tempBullet);
addChild(tempBullet);
//bullets movement forward
for(var i=bullets.length-1; i>=0; i--)
{
tempBullet = bullets[i];
tempBullet.y -= tempBullet.speed;
}
}
*/
/*----------------------------STATE_PLAY---------------------------*/
function playGame():void
{
//gameplay
//speedUp();
mapMove();
//fire();
makeBCivil();
makeBEnemy();
moveBCivil();
moveBEnemy();
vhDrops();
testCollision();
testForEnd();
removeExEplosions();
}
function mapMove(){
mp.y = mp.y - playerSpeed;
}
/*
function speedUp():void
{
var f:Number;
for (f<10; f++;)
{
var playerSpeed = playerSpeed + 1;
f = 0;
//mapMove();
MpRSimple = new MP_RS();
MpRSimple.y = MpRSimple.y - playerSpeed;
trace ("speed reset");
}
trace (f);
}
*/
function makeBCivil():void
{
//random chance
var chance:Number = Math.floor(Math.random()*BCRand);
if (chance <= 1 + level)
{
var tempBCivil:MovieClip;
//generate enemies
tempBCivil = new BCivil();
tempBCivil.speed = BCSpeed;
tempBCivil.x = Math.round(Math.random()*800);
addChild(tempBCivil);
civil.push(tempBCivil);
}
}
function moveBCivil():void
{
//move enemies
var tempBCivil:MovieClip;
for(var i:int = civil.length-1; i>=0; i--)
{
tempBCivil = civil[i];
tempBCivil.y += tempBCivil.speed
}
//testion colision with the player and screen out
if (tempBCivil.y > stage.stageHeight /* || tempBCivil.hitTestObject(player) */)
{
trace("ds hit");
//makeExplosion (player.x, player.y);
removeCivil(i);
//gameState = STATE_END;
}
}
//Test bullet colision
function testCollision():void
{
var tempBCivil:MovieClip;
var tempBEnemy:MovieClip;
var tempBullet:MovieClip;
//civil/bullet colision
civils:for(var i:int=civil.length-1; i>=0; i--)
{
tempBCivil = civil[i];
for (var j:int=bullets.length-1; j>=0; j--)
{
tempBullet = bullets[j];
if (tempBCivil.hitTestObject(tempBullet))
{
trace("laser hit the civil");
makeExplosion (tempBCivil.x, tempBCivil.y);
removeCivil(i);
removeBullet(j);
break civils;
}
}
}
//enemy/bullet colision
enemy:for(var k:int=enemy.length-1; k>=0; k--)
{
tempBEnemy = enemy[k];
for (var l:int=bullets.length-1; l>=0; l--)
{
tempBullet = bullets[l];
if (tempBEnemy.hitTestObject(tempBullet))
{
trace("bullet hit the Enemy");
makeExplosion (tempBEnemy.x, tempBEnemy.y);
removeEnemy(k);
removeBullet(l);
break enemy;
}
}
}
}
function makeExplosion(ex:Number, ey:Number):void
{
var tempExplosion:MovieClip;
tempExplosion = new boom();
tempExplosion.x = ex;
tempExplosion.y = ey;
addChild(tempExplosion)
explosions.push(tempExplosion);
}
function makeBEnemy():void
{
//random chance
var chance:Number = Math.floor(Math.random()*BCRand);
if (chance <= 1 + level)
{
var tempBEnemy:MovieClip;
//generate enemies
tempBEnemy = new BEnemy();
tempBEnemy.speed = BCSpeed;
tempBEnemy.x = Math.round(Math.random()*800);
addChild(tempBEnemy);
enemy.push(tempBEnemy);
}
}
function moveBEnemy():void
{
//move enemies
var tempBEnemy:MovieClip;
for(var i:int = enemy.length-1; i>=0; i--)
{
tempBEnemy = enemy[i];
tempBEnemy.y += tempBEnemy.speed
}
//testion colision with the player and screen out
if (tempBEnemy.y > stage.stageHeight /* || tempBCivil.hitTestObject(player) */)
{
trace("enemy");
//makeExplosion (player.x, player.y);
removeEnemy(i);
//gameState = STATE_END;
}
}
function vhDrops():void
{}
function testForEnd():void
{
//check damage
//end game
//gameState = STATE_END;
trace(gameState);
}
/*--------------------REMOVING BS FROM STAGE-----------------------*/
//removing civils
function removeCivil(idx:int):void
{
if(idx >= 0)
{
removeChild(civil[idx]);
civil.splice(idx, 1);
}
}
//removing enemies
function removeEnemy(idx:int):void
{
if(idx >= 0)
{
removeChild(enemy[idx]);
enemy.splice(idx, 1);
}
}
//removing civils
function removeBullet(idx:int):void
{
if(idx >= 0)
{
removeChild(bullets[idx]);
bullets.splice(idx, 1);
}
}
//removing expired explosions
function removeExEplosions():void
{
var tempExplosion:MovieClip;
for(var i=explosions.length-1; i>=0; i--)
{
tempExplosion = explosions[i];
if (tempExplosion.currentFrame >= tempExplosion.totalFrames)
{
removeExplosion(i);
}
}
}
//removing civils
function removeExplosion(idx:int):void
{
if(idx >= 0)
{
removeChild(explosions[idx]);
explosions.splice(idx, 1);
}
}
/*--------------------------STATE_END------------------------------*/
function endGame():void
{
}
/*gameScreen*/
/*endScreen*/
And Im sure theres some other bad code here but that doesent matter now.
Export for AS and AS Linkage is set:
http://i.stack.imgur.com/UvMAt.png
EDIT: removed the 2nd var declaration, still get the same error.
It would be great if you give more code and explanations, but here what I found about 1046 :
"One reason you will get this error is if you have an object on the stage with the same name as an object in your library."
Look at : http://curtismorley.com/2007/06/20/flash-cs3-flex-2-as3-error-1046/
Did you search any explanation on the Web for code 1046 ? Have you checked this one ?
I see that you have two var mp declaration. Try to remove the first one, var mp:MovieClip;. If it doesn't work, check your Flash Library carefully, be sure that MP_00 is a name available. If it's a MovieClip, check that it is exported for ActionScript. If it's an instance name, check that it's not used twice.
EDIT.
My suggestion is :
1- Change all references of mp to mp1. Maybe there is a conflict with an instance "...an object on the stage with the same name as an object in your library."
2- var mp1:MP_00; instead of var mp:MovieClip; in the declaration section.
3- Put the mp1 = new MP_00(); back in the startGame() function
4- Be sure that your new mp1 variable is everywhere you need and you have no more ref. to mp variable.
If... it's still doesn't work. I suggest : Change the name of your MovieClip linkage, like TestA, or whatever. Yes I know, a doubt on everything, but there is no magic, testing everything will show the problem for sure.
EDIT.
For mapMove() function...
First : be sure the speedUp() function is available, not in comments, and call it in your playGame() function. Your playerSpeed variable must have a value, so change : var playerSpeed = playerSpeed + 1; for playerSpeed = playerSpeed + 1;. Do not use var declaration twice for the same variable. (See var playerSpeed:Number; in header file.)
Beside... you have to know if the MP_00 clip on stage is YOUR mp1 clip. (I assumed you published all the code you have.)
Case A : MP_00 is on stage when you start your Clip.
If you actually see your MP_00 on screen, that mean you don't have to do mp1 = new MP_00(); and addchild(mp1);. It's already done for you (dragging a clip from library and giving a name instance, is the same as doing new and addchild).
Find the instance name (or give one). You should get your instance name and move your object (here the instance name is mp1):
mp1.y = mp1.y - playerSpeed;
Case B : MP_00 is NOT on stage when you start your Clip.
Do :
1- your declaration : var mp1:MP_00;
2- allocation memory : mp1 = new MP_00();
3- add to the display list : addchild(mp1);
4- "Shake it bb" : mp1.y = mp1.y - playerSpeed; or mp1.y -= playerSpeed;
I don't know what is your knowledge level exactly, so I tried to put everything. Sorry if it's too much.

How do I add Child when two instances collide?

I'm trying to create a virtual life simulation, where when a boy fish and a girl fish collide they reproduce. This is my first time using external class files and I'm quite confused.
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip
{
var numBalls:Number = 3;
var bFishList:Array = new Array();
var gFishList:Array = new Array();
public static var bFish:BoyFish = new BoyFish ;
public static var gFish:GirlFish = new GirlFish ;
var fishName:Object = bFish.name;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, enterFrameNow);
function enterFrameNow(event:Event):void {
// constructor code
for (var i = 0; i < numBalls; i++) {
var bFish:MovieClip = new BoyFish();
bFish.name = "bFish"+i;
addChild(bFish);
//variable ball a movieclip is a new instance of Ball
var gFish:MovieClip = new GirlFish();
// ball x and y value was in the middle of the stage
bFish.x = stage.stageWidth / 2;
bFish.y = stage.stageHeight / 2;
// add ball to stage
bFishList.push(bFish);
// ball x and y value was in the middle of the stage
gFish.x = stage.stageWidth / 2;
gFish.y = stage.stageHeight / 2;
// add ball to stage
addChild(gFish);
gFishList.push(gFish);
addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(event:Event):void {
for (i = 0; i < bFishList.length; i++) {
if (gFish.hitTestObject(bFishList[i]) == true) {
addChild(gFish);
gFishList.push(gFish);
trace("hit");
}
}
}
}
}
trace(bFishList);
trace(gFishList);
}
}
}
Here's the code from my Main.as file. I've tried for so long with the hitTestObject thing, and it's just not working for me.
Can anyone help me? I want it to add another instance to the stage when a bFish and a gFish collide.
You shouldn't add that many enterFrame events.
Add just one enterFrame handler, in your class, not as a local function, and add a nested loop into it.
//Somewhere in your Main() function :
addEventListener(Event.ENTER_FRAME, this.hitTest);
//In class Main
public function hitTest(event:Event):void
{
for (var i = 0; i < bFishList.length; i++)
{
for (var j = 0; j < gFishList.length; j++)
{
if (gFishList[j].hitTestObject(bFishList[i]))
{
var newFish:MovieClip = new GirlFish(); //Create it.
addChild(newFish);
gFishList.push(newFish);
}
}
}
}
Please note that if you aren't doing any more action, that could result of the creation of many girl fish each frame, which could eventually freeze the flashplayer.