PDA

View Full Version : removeChild removeMovieClip?



scott1969
April 13th, 2008, 11:49 PM
I am having difficulty removing my characters from the stage. I place code on the click event in my code but can't seem to get it to work. Could someone tell me where I am going wrong.

Thanks,
Scott

MAIN TIMELINE CODE:

[AS]

stop();

var score:int;// lynda.com Tut
var energy:int;// lynda.com Tut
var hits:int;// lynda.com Tut



//Code from lynda.com that is placed inside a MovieClip controlling energy..

/*function hurtPlayer(event:MotionEvent):void
{
var main:MovieClip = MovieClip(this.parent.parent);
main.decreaseEnergy();
this.parent.removeChild(this);
}

function die(event:MotionEvent):void
{
var main:MovieClip =MovieClip(this.parent.parent);
main.increaseScore();
this_animator.removeEventListener(MotionEvent.MOTI ON_END, hurtPlayer);
this.parent.removeChild(this);
}*/

//NEED to add text Field to test edu_Tut

/*var hits:int = 0;// tHits NEED to add text Field to test edu_Tut
var shots: int =0;// tshots NEED to add text Field to test edu_Tut
var duckCount = 0;// tDucks NEED to add text Field to test edu_Tut
var score:int = 0;// tScore NEED to convert to a graphic edu_Tut*/

/*var urlShot:URLRequest = new URLRequest("shot.mp3");//Need Sound effect edu_Tut
var sndShot:Sound = new Sound(urlShot);
var urlGotOne:URLRequest = new URLRequest("gotOne.mp3");//Need Sound effect edu_Tut
var sndGotOne:Sound = new Sound(urlGotOne);*/


var duckTimer:Timer = new Timer(3000, 1);
duckTimer.addEventListener(TimerEvent.TIMER, makeADuck);
duckTimer.start();


function makeADuck(evt:TimerEvent):void {
/*duckCount += 1;//extra from edu_Tut
tDucks.text = String(duckCount);//extra from edu_Tut */

var newDuck:Duck = new Duck();//(10) Point sys tPoints TEXT FEILD add to all Characters replace w/ movieClip
var duckIndex:Number = this.getChildIndex(gdCheet1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 204.8;
newDuck.swim();
//newDuck.disposition = "good";
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);



//Not Exactly Sure Where to Place This..
/*
score = 0;// lynda.com Tut
energy = energy_mc.totalFrames;//lynda.com Tut
energy_mc.gotoAndStop(energy);// lynda.com Tut*/
}


var duckTimer2:Timer = new Timer(35000,1);
duckTimer2.addEventListener(TimerEvent.TIMER, makeADuck2);
duckTimer2.start();

function makeADuck2(evt:TimerEvent):void {

var newDuck:Duck2 = new Duck2();
var duckIndex:Number = this.getChildIndex(duck2Fnl)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 178.2;
newDuck.swim();
//newDuck.disposition = "bad";
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}

var duckTimer3:Timer = new Timer(20000,1);
duckTimer3.addEventListener(TimerEvent.TIMER, makeADuck3);
duckTimer3.start();

function makeADuck3(evt:TimerEvent):void {

var newDuck:Duck3 = new Duck3();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 98.0;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);


}

var duckTimer4:Timer = new Timer(25000,1);
duckTimer4.addEventListener(TimerEvent.TIMER, makeADuck4);
duckTimer4.start();

function makeADuck4(evt:TimerEvent):void {

var newDuck:Duck4 = new Duck4();
var duckIndex:Number = this.getChildIndex(goodMnky2)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 147.6;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}

var duckTimer5:Timer = new Timer(25000,1);
duckTimer5.addEventListener(TimerEvent.TIMER, makeADuck5);
duckTimer5.start();

function makeADuck5(evt:TimerEvent):void {

var newDuck:Duck5 = new Duck5();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 58.1;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);


}

var duckTimer6:Timer = new Timer(27000,1);
duckTimer6.addEventListener(TimerEvent.TIMER, makeADuck6);
duckTimer6.start();

function makeADuck6(evt:TimerEvent):void {

var newDuck:Duck6 = new Duck6();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 389.6;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}



var duckTimer7:Timer = new Timer(15000,1);
duckTimer7.addEventListener(TimerEvent.TIMER, makeADuck7);
duckTimer7.start();

function makeADuck7(evt:TimerEvent):void {

var newDuck:Duck7 = new Duck7();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 374.6;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}

var duckTimer8:Timer = new Timer(30000,1);
duckTimer8.addEventListener(TimerEvent.TIMER, makeADuck8);
duckTimer8.start();

function makeADuck8(evt:TimerEvent):void {

var newDuck:Duck8 = new Duck8();
var duckIndex:Number = this.getChildIndex(bamboo1)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 485.5;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}

var duckTimer9:Timer = new Timer(8000,1);
duckTimer9.addEventListener(TimerEvent.TIMER, makeADuck9);
duckTimer9.start();

function makeADuck9(evt:TimerEvent):void {

var newDuck:Duck9 = new Duck9();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 385.4;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);

}

var duckTimer10:Timer = new Timer(25000,1);
duckTimer10.addEventListener(TimerEvent.TIMER, makeADuck10);
duckTimer10.start();

function makeADuck10(evt:TimerEvent):void {

var newDuck:Duck10 = new Duck10();
var duckIndex:Number = this.getChildIndex(goodMnky)+1;
addChildAt(newDuck, duckIndex);
newDuck.y = 352.3;
newDuck.swim();
newDuck.addEventListener(MouseEvent.CLICK, clickDuck);


}

// lynda.com Energy Bar "energy_mc"
/*
function increaseScore():void
{
score ++;
if(score >= monsterInGame)
{
monsterMaker.stop();
showWinLose("You Win!");

}
}


function decreaseEnergy():void
{
energy --;
if(energy <=0)
{
monsterMaker.stop();
showWinLose("You Lose!")
}
else
{
energy_mc.gotoAndStop(energy);
}
}


function showWinLose(endMessage:String):void
{
var winLose:MovieClip = new WinLose();
addChild(winLose);
winLose.end_txt.text = endMessage;
}
*/
function clickDuck(evt:MouseEvent):void {
evt.target.hit();
hits += 1;
if (hits == 30) {
gotoAndStop("splash");
}
trace(hits);
/*if (this.parent != null) {
this.parent.removeChild(duck);
}*/

if (evt.target.disposition == "bad") {
smileMeter_mc.prevFrame();
if (smileMeter_mc.currentFrame == 1) {
trace("You Are a Loser");
gotoAndStop("splash");

//duckTimer.removeEventListener(TimerEvent.TIMER, makeADuck);
//stage.removeEventListener(MouseEvent.MOUSE_MOVE,mo veTarget);
//duckTimer.stop();
}

} else {
smileMeter_mc.nextFrame();
if (smileMeter_mc.currentFrame == 20) {
trace("You Are a BadAss Winner");
gotoAndStop("splash");
}
}
trace("Click Duck!");
/*var rect:MovieClip = MovieClip (currentTarget);
this.removeChild(rect);
this.removeChild();*/

//removeEventListener(MovieClip, newDuck);
/*hits += 1;// Don't Need extra from edu_Tut
tHits.text = String(hits);// Don't Need extra from edu_Tut
score += evt.target.points;// score need to convert to energy_mc edu_Tut
tScore.text = String(score);// score need to convert to energy_mc edu_Tut
sndGotOne.play();// Need sound Effect edu_Tut
*/


/*function hitDuck(event:MouseEvent):void {
clickDuck.removeEventListener(MouseEvent.Mouse.CLI CK, MovieClip);*/
}

/*function anyClick(evt:MouseEvent):void
{
shots +=1;
tShots.text = String(shots);
sndShot.play(); //May not need this function. Nee Sound Effect edu_Tut
}
*/
//extra from edu_Tut
//stage.addEventListener(MouseEvent.CLICK, anyClick);//extra from edu_Tut

Mouse.hide();
mTarget.mouseEnabled = false;
function moveTarget(evt:MouseEvent):void {
//target graphic = mTargetget
mTarget.x = evt.stageX;
mTarget.y = evt.stageY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveT arget);

[AS]



EXTERNAL AS FILE:

[AS]

package {

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.motion.Animator;

public class Duck extends MovieClip {
var duckHit_animator:Animator;
public var disposition:String = "good";
//public var points:int;// form TUT


function Duck():void {
this.disposition = "good";

}
public function swim():void {
var swimTween = new Tween(this, "x",None.easeNone,4.8,4.8, 5, true);

}
public function hit():void {


var duckHit_xml:XML = <Motion duration="91" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">
<source>
<Source frameRate="24" x="245.15" y="586.9" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="duckHit">
<dimensions>
<geom:Rectangle left="-85.65" top="-51" width="171.3" height="102"/>
</dimensions>
<transformationPoint>
<geom:Point x="0.5046701692936368" y="0.5"/>
</transformationPoint>
</Source>
</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="5" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="56" blurY="56" color="0xFFCC00" alpha="1" strength="1" quality="3" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="10" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="85" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="90" tweenSnap="true" tweenSync="true">
<color>
<Color/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>
</Motion>;

duckHit_animator = new Animator(duckHit_xml, this);
duckHit_animator.play();
//removeMovieClip(this);




}
}
}
[AS]

wvxvw
April 14th, 2008, 05:50 AM
Can you shorten your code to the only problematic part? It's really difficult to grasp what's going on as long you use timeline scripts...

scott1969
April 14th, 2008, 04:48 PM
Sure. I also need to add sound when the good ducks are clicked and a sound when the bad ducks are click. I have several other AS files buct the one I am showing is representative of the others:
Thanks,
Scott

I have tried a few methods and nothing seem to work. I have to be placing the code in the wrong place. I also need to add a sound for the good ducks and a sound for the bad ducks when clicked. Any help would be appreciated.

Thanks,
Scott

Click Function on FLA file

[code]

function clickDuck(evt:MouseEvent):void {
evt.target.hit();
hits += 1;
if (hits == 30) {
gotoAndStop("splash");
}
trace(hits);
/*if (this.parent != null) {
this.parent.removeChild(duck);
}*/

if (evt.target.disposition == "bad") {
smileMeter_mc.prevFrame();
if (smileMeter_mc.currentFrame == 1) {
trace("You Are a Loser");
gotoAndStop("splash");

//duckTimer.removeEventListener(TimerEvent.TIMER, makeADuck);
//stage.removeEventListener(MouseEvent.MOUSE_MOVE,mo veTarget);
//duckTimer.stop();
}

} else {
smileMeter_mc.nextFrame();
if (smileMeter_mc.currentFrame == 20) {
trace("You Are a BadAss Winner");
gotoAndStop("splash");
}
}
trace("Click Duck!");
/*var rect:MovieClip = MovieClip (currentTarget);
this.removeChild(rect);
this.removeChild();*/

//removeEventListener(MovieClip, newDuck);
/*hits += 1;// Don't Need extra from edu_Tut
tHits.text = String(hits);// Don't Need extra from edu_Tut
score += evt.target.points;// score need to convert to energy_mc edu_Tut
tScore.text = String(score);// score need to convert to energy_mc edu_Tut
sndGotOne.play();// Need sound Effect edu_Tut
*/
[code]


And There is Public function on my external AS file:

public function hit():void {


var duckHit_xml:XML = <Motion duration="91" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">
<source>
<Source frameRate="24" x="245.15" y="586.9" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="duckHit">
<dimensions>
<geom:Rectangle left="-85.65" top="-51" width="171.3" height="102"/>
</dimensions>
<transformationPoint>
<geom:Point x="0.5046701692936368" y="0.5"/>
</transformationPoint>
</Source>
</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="5" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="56" blurY="56" color="0xFFCC00" alpha="1" strength="1" quality="3" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="10" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="85" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>

<Keyframe index="90" tweenSnap="true" tweenSync="true">
<color>
<Color/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
</filters>
</Keyframe>
</Motion>;

duckHit_animator = new Animator(duckHit_xml, this);
duckHit_animator.play();
//removeMovieClip(this);




}

wvxvw
April 14th, 2008, 05:23 PM
Ok... huh... I'm still not sure about what's not working, but, I've tested the Animator function, it's ok, (the pic blinks with yellow glow, than dissapears).
Is it a problem to call this function from... where? Meaning, the class containing it should be linked to the clip in the library, and the clip should have another clip on it's timeline called duckHit. Than, if you manage to get this clip's path (say, it's on the main timeline), you may call the function like this:
myDuckHitContainer.hit();

johnlouis
April 15th, 2008, 01:19 AM
add a / to closing [ as] tag.. [ as] [ /as]. i see smileys in your code. heh.
it's very hard to read now..

scott1969
April 17th, 2008, 05:32 PM
I do not have any of the animated movieClips(Duck, Duck2 ect..) on the main time line. I am placing and calling them from an external AS file.


[ as] package {

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.motion.Animator;

public class Duck extends MovieClip {
var duckHit_animator:Animator;
public var disposition:String = "good";
//public var points:int;// form TUT


function Duck():void {
this.disposition = "good";

}
public function swim():void {
var swimTween = new Tween(this, "x",None.easeNone,4.8,4.8, 5, true);

}
public function hit():void {

var duckHit_xml:XML = <Motion duration="75" xmlns="fl.motion.*" xmlns:geom="flash.geom.*" xmlns:filters="flash.filters.*">
<source>
<Source frameRate="24" x="245.15" y="586.9" scaleX="1" scaleY="1" rotation="0" elementType="movie clip" symbolName="duckHit">
<dimensions>
<geom:Rectangle left="-85.65" top="-51" width="171.3" height="102"/>
</dimensions>
<transformationPoint>
<geom:Point x="0.5046701692936368" y="0.5"/>
</transformationPoint>
</Source>
</source>

<Keyframe index="0" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="0" angle="45" color="0x000000" alpha="1" blurX="0" blurY="0" strength="0" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>

<Keyframe index="5" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="19" blurY="19" color="0xFF6600" alpha="1" strength="1" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="-2" angle="233" color="0xFF9900" alpha="1" blurX="0" blurY="0" strength="3.36" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>

<Keyframe index="9" tweenSnap="true" tweenSync="true">
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="19" blurY="19" color="0xFF6600" alpha="1" strength="1" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="-2" angle="233" color="0xFF9900" alpha="1" blurX="0" blurY="0" strength="3.36" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>

<Keyframe index="14" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="0" angle="45" color="0x000000" alpha="1" blurX="0" blurY="0" strength="0" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>

<Keyframe index="69" tweenSnap="true" tweenSync="true">
<color>
<Color alphaMultiplier="0"/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="0" angle="45" color="0x000000" alpha="1" blurX="0" blurY="0" strength="0" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>

<Keyframe index="74" tweenSnap="true" tweenSync="true">
<color>
<Color/>
</color>
<tweens>
<SimpleEase ease="0"/>
</tweens>
<filters>
<filters:GlowFilter blurX="0" blurY="0" color="0xFF0000" alpha="1" strength="0" quality="1" inner="false" knockout="false"/>
<filters:DropShadowFilter distance="0" angle="45" color="0x000000" alpha="1" blurX="0" blurY="0" strength="0" quality="1" inner="false" knockout="false" hideObject="false"/>
</filters>
</Keyframe>
</Motion>;

duckHit_animator = new Animator(duckHit_xml, this);
duckHit_animator.play();



//removeMovieClip(this);




}
}
}[ /as]

wvxvw
April 17th, 2008, 07:53 PM
Look... it took me some time to find this...

addChildAt(newDuck, duckIndex);

This function returns DisplayObject, which is prob your Duck-class. The other code you've posted is an input for Animator class, it describes the animation it should play when you call hit function, it doesn't add/remove the instance of the Duck to/from the stage...
So, add ducks you create into array, add indices to them, and once you have to need to remove it from the stage - find it in array by the index and use removeChild() on it...
BTW, dont put whitespaces in the BB tags.

scott1969
April 23rd, 2008, 06:01 PM
Cool. Thank you so much for your time.

Scott