Can't Remove MovieClip AS3

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 5194 Views - Last Post: 15 August 2013 - 05:36 AM

#1 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Can't Remove MovieClip AS3

Posted 12 August 2013 - 07:20 PM

I'm working on a group project and our "lead programmer" didn't quite know what he was doing and didn't bother to ask so he formatted our assignment a bit...strange. It's working fine so far but a lot of things are broken due to a lack of an overall package (i know, it's a facepalm) anyway, i'm trying to remove a movieclip without the ability to you removeMovieClip.()

Yes it really is just one big jumble and yes, everything is working for the most part. The main problem is located in the coin function. Can't remove the coins, so it constantly updates our score. Unfortunately it's too late for us to completely reformat. :( Thank you for any suggestions

So here's my code

import flash.events.MouseEvent;
import flash.net.URLRequest;

playButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);

function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
	gotoAndPlay("Level One");
	levelOneTrue();
	levelTwoTrue();
}

Link.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
	navigateToURL(new URLRequest("http://www.namihelpsyouth.org"));
}

import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.text.TextField;

var animationState:String = "idle";

var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var spacePressed:Boolean = false;
var ePressed:Boolean = false;

var scrollX:Number = 1015.80;
var scrollY:Number = 117.15;
var xSpeed:Number = 0;
var ySpeed:Number = 0;

var speedConstant:int = 5;
var frictionConstant:Number = 0.9;

var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;

var leftBumpPoint:Point = new Point(-15, 0);
var rightBumpPoint:Point = new Point(15, 0);
var upBumpPoint:Point = new Point(0, -20);
var downBumpPoint:Point = new Point(0, 31.5);

var gravityConstant:Number = 1.5;
var jumpConstant:Number = -35;
var maxSpeedConstant:Number = 18;

var currentLevel:int = 1;

var currentScore:Number = 0;

var audio:landing = new landing();

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

function keyDownHandler(e:KeyboardEvent):void{
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.A){
		leftPressed = true;
	} else if(e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.D){
		rightPressed = true;
 
	} else if(e.keyCode == Keyboard.UP || e.keyCode == Keyboard.W){
		upPressed = true;
		
		}
 
	 else if(e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.S){
		downPressed = true;
	}
	else if(e.keyCode == Keyboard.SPACE){
		spacePressed = true;
	} 
	else if(e.keyCode == Keyboard.E){
		ePressed = true;
		if(player.hitTestObject(tony)){
			nextLevel();
		}
	}
}

function keyUpHandler(e:KeyboardEvent):void{
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.A){
		leftPressed = false;
 
	} else if(e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.D){
		rightPressed = false;
 
	} else if(e.keyCode == Keyboard.UP || e.keyCode == Keyboard.W){
		upPressed = false;
 
	} else if(e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.S){
		downPressed = false;
	}
	else if(e.keyCode == Keyboard.SPACE){
		spacePressed = false;
	} else if(e.keyCode == Keyboard.E){
		ePressed = false;
	}
}

function levelOneCollisionTrue():void{
	if(back1.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
    leftBumping = true;
	} else {
    leftBumping = false;
	}
 
	if(back1.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
    rightBumping = true;
	} else {
    rightBumping = false;
	}
 /*
	if(back1.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
    upBumping = true;
	} else {
    upBumping = false;
	}
 */
	if(back1.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
    downBumping = true;
	} else {
    downBumping = false;
	}
}

function kill():void{
	if (player.hitTestObject(killbox1) && currentLevel == 1)
	{
		gotoLevel1();
	}
	if(player.hitTestObject(killbox1) && currentLevel == 2){
		gotoLevel2();
	}
	if(player.hitTestObject(killbox1) && currentLevel == 3){
		gotoLevel3();
	}
}

function coin():void{
	
	if (player.hitTestObject(killbox1) && currentLevel == 1)
	{
		Coin1.visible = true;
		Coin2.visible = true;
		Coin3.visible = true;
		Coin4.visible = true;
		Coin5.visible = true;
		Coin6.visible = true;
		Coin7.visible = true;
		Coin8.visible = true;
		Coin9.visible = true;
		Coin10.visible = true;
		Coin11.visible = true;
		Coin12.visible = true;
		Coin13.visible = true;
		Coin14.visible = true;
		Coin15.visible = true;
		Coin16.visible = true;
		Coin17.visible = true;
		Coin18.visible = true;
		Coin19.visible = true;
		Coin20.visible = true;
		Coin21.visible = true;
		Coin22.visible = true;
		
		currentScore = 0;
		scorebox.text = String(currentScore);
	}
	
	if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		delete(Coin1);

	}
	
	if(player.hitTestObject(Coin2)){
		trace("pickup");
		Coin2.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin3)){
		trace("pickup");
		Coin3.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin4)){
		trace("pickup");
		Coin4.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin5)){
		trace("pickup");
		Coin5.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin6)){
		trace("pickup");
		Coin6.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin7)){
		trace("pickup");
		Coin7.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin8)){
		trace("pickup");
		Coin8.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin9)){
		trace("pickup");
		Coin9.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin10)){
		trace("pickup");
		Coin10.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin11)){
		trace("pickup");
		Coin11.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin12)){
		trace("pickup");
		Coin12.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin13)){
		trace("pickup");
		Coin13.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin14)){
		trace("pickup");
		Coin14.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin15)){
		trace("pickup");
		Coin15.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin16)){
		trace("pickup");
		Coin16.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin17)){
		trace("pickup");
		Coin17.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin18)){
		trace("pickup");
		Coin18.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin19)){
		trace("pickup");
		Coin19.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin20)){
		trace("pickup");
		Coin20.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin21)){
		trace("pickup");
		Coin21.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin22)){
		trace("pickup");
		Coin22.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
}

function levelOneTrue():void{
	back1.visible = true;
	//coins.visible = true;
	trees.visible = true;
	clouds.visible = true;
	clouds2.visible = true;
	moon.visible = true;
	sky.visible = true;
	tony.visible = true;
}

function levelOneFalse():void{
	//coins.visible = false;
	trees.visible = false;
	clouds.visible = false;
	clouds2.visible = false;
	moon.visible = false;
	sky.visible = false;
	}
	
function levelTwoTrue():void{
	sun.visible = true;
	clouds3.visible = true;
	ground2.visible = true;
	pyramids.visible = true;
	sky2.visible = true;
}

function levelTwoFalse():void{
	sun.visible = false;
	clouds3.visible = false;
	ground2.visible = false;
	pyramids.visible = false;
	sky2.visible = false;
}


function nextLevel():void{
	currentLevel++;
	trace("Next Level: " + currentLevel);
	if(currentLevel == 2){
	   gotoLevel2();
	} else if (currentLevel == 3){
		gotoLevel3();
	}
}

function gotoLevel1():void{
	currentLevel == 1;
	back1.gotoAndStop(1);
	scrollX = 1015.80;
	scrollY = 117.15;
}

function gotoLevel2():void{
	back1.gotoAndStop(2);
	gotoAndPlay("Level Two");
	levelOneFalse();
	scrollX = 1015.80;
	scrollY = 117.15;
}

function gotoLevel3():void{
	back1.gotoAndStop(3);
	gotoAndPlay("Level Three");
	levelOneFalse();
	levelTwoFalse();
	scrollX = 1015.80;
	scrollY = 117.15;
}

stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void{

	pressE.visible = false;
	levelOneCollisionTrue();
	kill();
	coin();
	
	if(player.hitTestObject(tony)){
		pressE.visible = true;
	}
	
	killbox1.visible = false;

	if(leftPressed){
        xSpeed -= speedConstant;
		player.scaleX = -1.8;
    } else if(rightPressed){
        xSpeed += speedConstant;
		player.scaleX = 1.8;
    }

     /*if(upPressed){
        ySpeed -= speedConstant; 
    } else if(downPressed){
        ySpeed += speedConstant; 
    }*/
	
	//Collision
	if(leftBumping){
    if(xSpeed < 0){
        xSpeed *= -0.5;
    }
}

if(leftBumping){
	if(xSpeed < 0){
		xSpeed *= -0.5;
	}
}
 
if(rightBumping){
    if(xSpeed > 0){
        xSpeed *= -0.5;
    }
}
 
if(upBumping){
    if(ySpeed < 0){
        ySpeed *= -0.5;
    }
}
 
if(downBumping){
    if(ySpeed > 0){
        ySpeed = 0;
		audio.play();
    }
	if(spacePressed || upPressed){
		ySpeed = jumpConstant;
	}
} else {
	ySpeed += gravityConstant;
}

	if(xSpeed > maxSpeedConstant){
		xSpeed = maxSpeedConstant;
	} else if(xSpeed < (maxSpeedConstant * -1)){
		xSpeed = (maxSpeedConstant * -1);
	}
	
	xSpeed *= frictionConstant;
	ySpeed *= frictionConstant;
	
	if(Math.abs(xSpeed) < 0.5){
		xSpeed = 0;
	}
	
	scrollX -= xSpeed;
	scrollY -= ySpeed;
	
	//Level one scrolling
	back1.x = scrollX;
	back1.y = scrollY;

	moon.x = scrollX * 0.1;
	moon.y = scrollY * 0.1;
	
	clouds.x = scrollX * 0.2;
	clouds.y = scrollY * 0.2;
	
	clouds2.x = scrollX * 0.35;
	clouds2.y = scrollY * 0.29;
	
	trees.x = scrollX * 0.5;
	trees.y = scrollY * 0.5;

	water.x = scrollX;
	water.y = scrollY;
	
	killbox1.x = scrollX;
	killbox1.y = scrollY;
	
    Coin1.x = scrollX;
	Coin1.y = scrollY;
	
	Coin2.x = scrollX;
	Coin2.y = scrollY;
	
	Coin3.x = scrollX;
	Coin3.y = scrollY;
	
	Coin4.x = scrollX;
	Coin4.y = scrollY;
	
	Coin5.x = scrollX;
	Coin5.y = scrollY;
	
	Coin6.x = scrollX;
	Coin6.y = scrollY;
	
	Coin7.x = scrollX;
	Coin7.y = scrollY;
	
	Coin8.x = scrollX;
	Coin8.y = scrollY;
	
	Coin9.x = scrollX;
	Coin9.y = scrollY;
	
	Coin10.x = scrollX;
	Coin10.y = scrollY;
	
	Coin11.x = scrollX;
	Coin11.y = scrollY;
	
	Coin12.x = scrollX;
	Coin12.y = scrollY;
	
	Coin13.x = scrollX;
	Coin13.y = scrollY;
	
	Coin14.x = scrollX;
	Coin14.y = scrollY;
	
	Coin15.x = scrollX;
	Coin15.y = scrollY;
	
	Coin16.x = scrollX;
	Coin16.y = scrollY;
	
	Coin17.x = scrollX;
	Coin17.y = scrollY;
	
	Coin18.x = scrollX;
	Coin18.y = scrollY;
	
	Coin19.x = scrollX;
	Coin19.y = scrollY;
	
	Coin20.x = scrollX;
	Coin20.y = scrollY;
	
	Coin21.x = scrollX;
	Coin21.y = scrollY;
	
	Coin22.x = scrollX;
	Coin22.y = scrollY;
	
	tony.x = scrollX;
	tony.y = scrollY;
	
	//Level two scrolling
	
	sun.x = scrollX * 0.1;
	sun.y = scrollY * 0.5;
	
	clouds3.x = scrollX * 0.2;
	clouds3.y = scrollY * 0.2;
	
	ground2.x = scrollX * 0.5;
	ground2.y = scrollY * 0.5;
	
	pyramids.x = scrollX * 0.3;
	pyramids.y = scrollY * 0.5;

	//Level three scrolling	
	sun2.x = scrollX * 0.2;
	sun2.y = scrollY * 0.2;
	
	ground3.x = scrollX * 0.5;
	ground3.y = scrollY * 0.5;
	
	sphinx.x = scrollX * 0.5;
	sphinx.y = scrollY * 0.5;
	
	pyramid2.x = scrollX * 0.6;
	pyramid2.y = scrollY * 0.5;

		if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
		animationState = "running";
	} else if(downBumping){
		animationState = "idle";
	}else {
		animationState = "falling";
	}
	if(upPressed){
		animationState = "jumping";
	} 
	if(player.hitTestObject(water)){
		animationState = "drowning";
	}

		if(player.currentLabel != animationState){
		player.gotoAndStop(animationState);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Can't Remove MovieClip AS3

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 01:47 AM

Moved to Flash & Actionscript

Are the coins on the stage? I don't see them being created dynamically, which means
delete
won't work.
Was This Post Helpful? 1
  • +
  • -

#3 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 05:16 AM

Yes, apparently I'm a complete noob. I thought i'd deleted that before posting my code on here. I'm sorry for wasting your time with that. You're completely correct it doesn't work.

The main problem for us is that because the code is not formatted properly, conventional methods of removing the coins doesn't work, which is why we're just making them invisible for now. We need an unconventional method to remove them and unfortunately we can't find any through any of the sites we've been checking, or any of our instructors.
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 06:03 AM

You could quite easily just remove the coins from the stage

stage.removeChild(Coin1);



and then put them up for garbage collection

Coin1 = null;



You will have to check whether the coin is there before attempting anything with it though

if(Coin1)
{
        if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		delete(Coin1);

	}
}



This all assumes that the coins are on the stage in the first place. If not then you will have to find out what they are a child of and remove them from that.
Was This Post Helpful? 1
  • +
  • -

#5 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 06:48 AM

Thank you. When I implemented this use of removeChild, it didn't error out, but when I came in contact with the coin it did. The error I got is as follows:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at De_fla::MainTimeline/coin()
at De_fla::MainTimeline/loop()
pickup

Now I mostly understand what I'm reading but I'm not really a programmer so I don't fully understand. I'm assuming it's telling me that I'm removing the child from something that is not it's parent, please correct me if I'm wrong, and that I must change the removeChild so that it is in the correct location.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 06:54 AM

Well try

parent.removeChild(Coin1);


Was This Post Helpful? 1
  • +
  • -

#7 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 06:58 AM

Thank you for the suggestion unfortunately that does not work either. It gives the same error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at De_fla::MainTimeline/coin()
at De_fla::MainTimeline/loop()
Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:06 AM

Ok, well you are going to have to give me some more information about how this is setup.

Is this all on the timeline?

Actually I see that it is. One more thing that may work is to use the coin to get the parent

MovieClip(Coin1).parent.removeChild(Coin1);


Was This Post Helpful? 1
  • +
  • -

#9 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:13 AM

Yes, i'm sorry for not including that before, we have been setting up everything on the timeline.

Again it has not worked, however it is giving me a new error,(the error only pops up when the player comes in contact with the coin) that may possibly help direct you in helping me.

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at De_fla::MainTimeline/coin()
at De_fla::MainTimeline/loop()

Now, I know what a null object is, but i'm not sure what it might be referring to unless it's referring to the movie clip that i'm hitting.
Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:14 AM

Paste what you have so far please
Was This Post Helpful? 1
  • +
  • -

#11 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:18 AM

the coin code is as follows:

 if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		MovieClip(Coin1).parent.removeChild(Coin1);

	}



the full code is as follows:


import flash.events.MouseEvent;
import flash.net.URLRequest;

playButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame);

function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
	gotoAndPlay("Level One");
	levelOneTrue();
	levelTwoTrue();
}

Link.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void{
	navigateToURL(new URLRequest("http://www.namihelpsyouth.org"));
}

import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.text.TextField;

var animationState:String = "idle";

var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var spacePressed:Boolean = false;
var ePressed:Boolean = false;

var scrollX:Number = 1015.80;
var scrollY:Number = 117.15;
var xSpeed:Number = 0;
var ySpeed:Number = 0;

var speedConstant:int = 5;
var frictionConstant:Number = 0.9;

var leftBumping:Boolean = false;
var rightBumping:Boolean = false;
var upBumping:Boolean = false;
var downBumping:Boolean = false;

var leftBumpPoint:Point = new Point(-15, 0);
var rightBumpPoint:Point = new Point(15, 0);
var upBumpPoint:Point = new Point(0, -20);
var downBumpPoint:Point = new Point(0, 31.5);

var gravityConstant:Number = 1.5;
var jumpConstant:Number = -35;
var maxSpeedConstant:Number = 18;

var currentLevel:int = 1;

var currentScore:Number = 0;

var audio:landing = new landing();

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

function keyDownHandler(e:KeyboardEvent):void{
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.A){
		leftPressed = true;
	} else if(e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.D){
		rightPressed = true;
 
	} else if(e.keyCode == Keyboard.UP || e.keyCode == Keyboard.W){
		upPressed = true;
		
		}
 
	 else if(e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.S){
		downPressed = true;
	}
	else if(e.keyCode == Keyboard.SPACE){
		spacePressed = true;
	} 
	else if(e.keyCode == Keyboard.E){
		ePressed = true;
		if(player.hitTestObject(tony)){
			nextLevel();
		}
	}
}

function keyUpHandler(e:KeyboardEvent):void{
	if(e.keyCode == Keyboard.LEFT || e.keyCode == Keyboard.A){
		leftPressed = false;
 
	} else if(e.keyCode == Keyboard.RIGHT || e.keyCode == Keyboard.D){
		rightPressed = false;
 
	} else if(e.keyCode == Keyboard.UP || e.keyCode == Keyboard.W){
		upPressed = false;
 
	} else if(e.keyCode == Keyboard.DOWN || e.keyCode == Keyboard.S){
		downPressed = false;
	}
	else if(e.keyCode == Keyboard.SPACE){
		spacePressed = false;
	} else if(e.keyCode == Keyboard.E){
		ePressed = false;
	}
}

function levelOneCollisionTrue():void{
	if(back1.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){
    leftBumping = true;
	} else {
    leftBumping = false;
	}
 
	if(back1.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){
    rightBumping = true;
	} else {
    rightBumping = false;
	}
 /*
	if(back1.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){
    upBumping = true;
	} else {
    upBumping = false;
	}
 */
	if(back1.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y, true)){
    downBumping = true;
	} else {
    downBumping = false;
	}
}

function kill():void{
	if (player.hitTestObject(killbox1) && currentLevel == 1)
	{
		gotoLevel1();
	}
	if(player.hitTestObject(killbox1) && currentLevel == 2){
		gotoLevel2();
	}
	if(player.hitTestObject(killbox1) && currentLevel == 3){
		gotoLevel3();
	}
}

function coin():void{
	
	if (player.hitTestObject(killbox1) && currentLevel == 1)
	{
		Coin1.visible = true;
		Coin2.visible = true;
		Coin3.visible = true;
		Coin4.visible = true;
		Coin5.visible = true;
		Coin6.visible = true;
		Coin7.visible = true;
		Coin8.visible = true;
		Coin9.visible = true;
		Coin10.visible = true;
		Coin11.visible = true;
		Coin12.visible = true;
		Coin13.visible = true;
		Coin14.visible = true;
		Coin15.visible = true;
		Coin16.visible = true;
		Coin17.visible = true;
		Coin18.visible = true;
		Coin19.visible = true;
		Coin20.visible = true;
		Coin21.visible = true;
		Coin22.visible = true;
		
		currentScore = 0;
		scorebox.text = String(currentScore);
	}
	
	if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		MovieClip(Coin1).parent.removeChild(Coin1);

	}
	
	if(player.hitTestObject(Coin2)){
		trace("pickup");
		Coin2.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin3)){
		trace("pickup");
		Coin3.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin4)){
		trace("pickup");
		Coin4.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin5)){
		trace("pickup");
		Coin5.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin6)){
		trace("pickup");
		Coin6.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin7)){
		trace("pickup");
		Coin7.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin8)){
		trace("pickup");
		Coin8.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin9)){
		trace("pickup");
		Coin9.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin10)){
		trace("pickup");
		Coin10.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin11)){
		trace("pickup");
		Coin11.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin12)){
		trace("pickup");
		Coin12.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin13)){
		trace("pickup");
		Coin13.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin14)){
		trace("pickup");
		Coin14.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin15)){
		trace("pickup");
		Coin15.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin16)){
		trace("pickup");
		Coin16.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin17)){
		trace("pickup");
		Coin17.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin18)){
		trace("pickup");
		Coin18.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin19)){
		trace("pickup");
		Coin19.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin20)){
		trace("pickup");
		Coin20.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin21)){
		trace("pickup");
		Coin21.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
	
	if(player.hitTestObject(Coin22)){
		trace("pickup");
		Coin22.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		
	}
}

function levelOneTrue():void{
	back1.visible = true;
	//coins.visible = true;
	trees.visible = true;
	clouds.visible = true;
	clouds2.visible = true;
	moon.visible = true;
	sky.visible = true;
	tony.visible = true;
}

function levelOneFalse():void{
	//coins.visible = false;
	trees.visible = false;
	clouds.visible = false;
	clouds2.visible = false;
	moon.visible = false;
	sky.visible = false;
	}
	
function levelTwoTrue():void{
	sun.visible = true;
	clouds3.visible = true;
	ground2.visible = true;
	pyramids.visible = true;
	sky2.visible = true;
}

function levelTwoFalse():void{
	sun.visible = false;
	clouds3.visible = false;
	ground2.visible = false;
	pyramids.visible = false;
	sky2.visible = false;
}


function nextLevel():void{
	currentLevel++;
	trace("Next Level: " + currentLevel);
	if(currentLevel == 2){
	   gotoLevel2();
	} else if (currentLevel == 3){
		gotoLevel3();
	}
}

function gotoLevel1():void{
	currentLevel == 1;
	back1.gotoAndStop(1);
	scrollX = 1015.80;
	scrollY = 117.15;
}

function gotoLevel2():void{
	back1.gotoAndStop(2);
	gotoAndPlay("Level Two");
	levelOneFalse();
	scrollX = 1015.80;
	scrollY = 117.15;
}

function gotoLevel3():void{
	back1.gotoAndStop(3);
	gotoAndPlay("Level Three");
	levelOneFalse();
	levelTwoFalse();
	scrollX = 1015.80;
	scrollY = 117.15;
}

stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event):void{

	pressE.visible = false;
	levelOneCollisionTrue();
	kill();
	coin();
	
	if(player.hitTestObject(tony)){
		pressE.visible = true;
	}
	
	killbox1.visible = false;

	if(leftPressed){
        xSpeed -= speedConstant;
		player.scaleX = -1.8;
    } else if(rightPressed){
        xSpeed += speedConstant;
		player.scaleX = 1.8;
    }

     /*if(upPressed){
        ySpeed -= speedConstant; 
    } else if(downPressed){
        ySpeed += speedConstant; 
    }*/
	
	//Collision
	if(leftBumping){
    if(xSpeed < 0){
        xSpeed *= -0.5;
    }
}

if(leftBumping){
	if(xSpeed < 0){
		xSpeed *= -0.5;
	}
}
 
if(rightBumping){
    if(xSpeed > 0){
        xSpeed *= -0.5;
    }
}
 
if(upBumping){
    if(ySpeed < 0){
        ySpeed *= -0.5;
    }
}
 
if(downBumping){
    if(ySpeed > 0){
        ySpeed = 0;
		audio.play();
    }
	if(spacePressed || upPressed){
		ySpeed = jumpConstant;
	}
} else {
	ySpeed += gravityConstant;
}

	if(xSpeed > maxSpeedConstant){
		xSpeed = maxSpeedConstant;
	} else if(xSpeed < (maxSpeedConstant * -1)){
		xSpeed = (maxSpeedConstant * -1);
	}
	
	xSpeed *= frictionConstant;
	ySpeed *= frictionConstant;
	
	if(Math.abs(xSpeed) < 0.5){
		xSpeed = 0;
	}
	
	scrollX -= xSpeed;
	scrollY -= ySpeed;
	
	//Level one scrolling
	back1.x = scrollX;
	back1.y = scrollY;

	moon.x = scrollX * 0.1;
	moon.y = scrollY * 0.1;
	
	clouds.x = scrollX * 0.2;
	clouds.y = scrollY * 0.2;
	
	clouds2.x = scrollX * 0.35;
	clouds2.y = scrollY * 0.29;
	
	trees.x = scrollX * 0.5;
	trees.y = scrollY * 0.5;

	water.x = scrollX;
	water.y = scrollY;
	
	killbox1.x = scrollX;
	killbox1.y = scrollY;
	
    Coin1.x = scrollX;
	Coin1.y = scrollY;
	
	Coin2.x = scrollX;
	Coin2.y = scrollY;
	
	Coin3.x = scrollX;
	Coin3.y = scrollY;
	
	Coin4.x = scrollX;
	Coin4.y = scrollY;
	
	Coin5.x = scrollX;
	Coin5.y = scrollY;
	
	Coin6.x = scrollX;
	Coin6.y = scrollY;
	
	Coin7.x = scrollX;
	Coin7.y = scrollY;
	
	Coin8.x = scrollX;
	Coin8.y = scrollY;
	
	Coin9.x = scrollX;
	Coin9.y = scrollY;
	
	Coin10.x = scrollX;
	Coin10.y = scrollY;
	
	Coin11.x = scrollX;
	Coin11.y = scrollY;
	
	Coin12.x = scrollX;
	Coin12.y = scrollY;
	
	Coin13.x = scrollX;
	Coin13.y = scrollY;
	
	Coin14.x = scrollX;
	Coin14.y = scrollY;
	
	Coin15.x = scrollX;
	Coin15.y = scrollY;
	
	Coin16.x = scrollX;
	Coin16.y = scrollY;
	
	Coin17.x = scrollX;
	Coin17.y = scrollY;
	
	Coin18.x = scrollX;
	Coin18.y = scrollY;
	
	Coin19.x = scrollX;
	Coin19.y = scrollY;
	
	Coin20.x = scrollX;
	Coin20.y = scrollY;
	
	Coin21.x = scrollX;
	Coin21.y = scrollY;
	
	Coin22.x = scrollX;
	Coin22.y = scrollY;
	
	tony.x = scrollX;
	tony.y = scrollY;
	
	//Level two scrolling
	
	sun.x = scrollX * 0.1;
	sun.y = scrollY * 0.5;
	
	clouds3.x = scrollX * 0.2;
	clouds3.y = scrollY * 0.2;
	
	ground2.x = scrollX * 0.5;
	ground2.y = scrollY * 0.5;
	
	pyramids.x = scrollX * 0.3;
	pyramids.y = scrollY * 0.5;

	//Level three scrolling	
	sun2.x = scrollX * 0.2;
	sun2.y = scrollY * 0.2;
	
	ground3.x = scrollX * 0.5;
	ground3.y = scrollY * 0.5;
	
	sphinx.x = scrollX * 0.5;
	sphinx.y = scrollY * 0.5;
	
	pyramid2.x = scrollX * 0.6;
	pyramid2.y = scrollY * 0.5;

		if( ( leftPressed || rightPressed || xSpeed > speedConstant || xSpeed < speedConstant *-1 ) && downBumping){
		animationState = "running";
	} else if(downBumping){
		animationState = "idle";
	}else {
		animationState = "falling";
	}
	if(upPressed){
		animationState = "jumping";
	} 
	if(player.hitTestObject(water)){
		animationState = "drowning";
	}

		if(player.currentLabel != animationState){
		player.gotoAndStop(animationState);
	}
}



One other thing that I just noticed that might help is that Coin1 is just the instance name. The movie clip name is "coins1". I'm not quite sure why he has them named differently from each other though.
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:23 AM

You are not checking for null on the hit test like i described

if(Coin1) // check for null
{
        if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		delete(Coin1);

	}
}



Calling hitTestObject on a null reference will throw this error
Was This Post Helpful? 0
  • +
  • -

#13 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:30 AM

My bad, I guess I didn't fully understand your instruction at the time (still new to programming).

ok so i updated the code and i'm not entirely sure I did it right. Anyway here is the code now

if(Coin1){
		trace("null object")
	if(player.hitTestObject(Coin1)){
		trace("pickup");
		Coin1.visible = false;
		currentScore += 10;
		scorebox.text = String(currentScore);
		MovieClip(Coin1).parent.removeChild(Coin1);

	     }
	}


Was This Post Helpful? 0
  • +
  • -

#14 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:41 AM

Essentially the check is if the coin is not null

if(Coin1)



Go ahead and hittest it otherwise move on.
Was This Post Helpful? 1
  • +
  • -

#15 llamaboy226  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-August 13

Re: Can't Remove MovieClip AS3

Posted 13 August 2013 - 07:53 AM

The coin is null. When I used the trace along with the
 if(Coin1) 
it responded telling me the coin is null.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2