Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem removing 1 of 8 generated collisions

Status
Not open for further replies.

Marleyuk

Technical User
Feb 10, 2006
25
GB
Hello, im a bit of a flash amature but im having ago at creating a small game, the graphics are pretty awful but it seems to work. The problem im having is I use the following

Code:
numEnemy=8; for (i=2; i<=numEnemy; i++){ enemy1.duplicateMovieClip( "enemy"+i, i+100 ); } score=0;

To duplicate the enemy 8 times, 7 of them can be shot and removed but 1 wont disappear. I think i may have to remove the original enemy but im not sure where. Can anyone help please.



Thanks,
Marls.

This post has been edited b
 
Are you removing your MovieClips with "removeMovieClip()"? If so "removeMovieClip()" cannot remove MovieClips sitting in the negative depth. Did you place your MovieClip "enemy1" on Stage manually? If so "enemy1" sits in a negative depth, something like -16383, so it cannot removed by "removeMovieClip()".

One solution is to "swapDepths()" to 0 or higher depth before remove it.

But more elegant solution would be to use "attachMovie()" in the first place instead of "duplicateMovieClip()" so that you can assign the depth of your choice to all 8 of your MovieClips.

Kenneth Kawamoto
 
Thanks for the reply, How would i replace "duplicateMovieClip()" with "attachMovie()" and still generate 8 enemies?
 
Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
	this.attachMovie("enemy", "enemy"+i, 100+i);
}
This will dynamically create 8 enemies (enemy1 - enemy8) on Stage at depth 101 - 108. You have to have a MovieClip Symbol in the Library with its Linkage Identifier set to "enemy". You don't need anything on Stage.


Kenneth Kawamoto
 
Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
    this.attachMovie("enemy", "enemy"+i, 100+i);
}

This spawns one bad guy and positions it in a stationary position in the top right hand corner of the screen, the enemey needs to be reproduced and move around from the right side of the screen towards the tank on the left.

Any help pleasE?

Thanks,
Marls.
 
Well you can place the MovieClip anywhere on Stage and move them anyway you like.

This will place 8 "enemy" MovieClips on the right hand side of the Stage with 30px in between, then move them towards left.
Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
	var enemyMC:MovieClip = this.attachMovie("enemy", "enemy"+i, 100+i, {_x:Stage.width-50, _y:30*i});
	enemyMC.onEnterFrame = function():Void  {
		this._x--;
	};
}

Kenneth Kawamoto
 
It still is only giving me one enemy now. Is it right that im putting this in the enemy movie clip action script?
 
I have put this in the background scene which was the first scene of the movie and this is the only thing in it.

Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
    var enemyMC:MovieClip = this.attachMovie("enemy", "enemy"+i, 100+i, {_x:Stage.width-50, _y:30*i});
    enemyMC.onEnterFrame = function():Void  {
        this._x--;
    };
}

I have a feeling this may be what is causing my problems

Code:
onClipEvent (load) {
 function reset(){
 this._x=300;
 this._y=random(200)+100;
 enemySpeed=random(4)+1;
 this.gotoAndStop(1);
 }
 reset();
}

onClipEvent (enterFrame) {
 enemy1.duplicateMovieClip();
 if (_root.tank_mc.scrollStart){
 this._x-=enemySpeed+_root.mainGround.groundSpeed;
 } else {
 this._x-=enemySpeed;
 }
 if (this._x<-10) {
 reset();
 }
}

That is the code for my enemy planes, i have in the enemy_mc movie clip action script. When run the code produces only one plane now and its doesnt disappear when i shoot it.

Sorry if im missing something obvious, can you see any problem with what ive got?
 
1. Remove your "enemy" MovieClip from the Stage.
2. Set the Linkage Identifier of "enemy" MovieClip Symbol to "enemy"
Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
	mcEnemy = this.attachMovie("enemy", "enemy"+i, 100+i);
	reset(mcEnemy);
	mcEnemy.onEnterFrame = moveEnemy;
}
function reset(mc:MovieClip):Void {
	mc._x = 300;
	mc._y = Math.floor((Math.random()*200+100));
	mc.enemySpeed = Math.floor(Math.random()*4+1);
}
function moveEnemy():Void {
	if (tank_mc.scrollStart) {
		this._x -= this.enemySpeed+mainGround.groundSpeed;
	} else {
		this._x -= this.enemySpeed;
	}
	if (this._x<-10) {
		reset(this);
	}
}


Kenneth Kawamoto
 
I swear i couldnt tell you how much you have just helped me, thanks so much.

..although i do have another problem.

Code:
onClipEvent (load) {
 hpAmount = 100;
}
onClipEvent (enterFrame) {
 this._width = _root.hpAmount;
 if (_root.hpAmount>100) {
 _root.hpAmount = 100;
 }
 if (_root.hpAmount<0) {
 _root.hpAmount= 0;
 _root.gotoAndStop("gameOver");
 }
 if (this._width<_root.hpAmount) {
 this._xscale = _root.hpAmount;
 }
 if (this._width>_root.hpAmount) {
 this._xscale = _root.hpAmount;
 }
}

That is my health bar i want to make for my tank. When an enemy hits it the health should start at 100 and decrease. That should control the metre but how to add i the hit test to the tank so it knows to take points of the hp bar when the tank is hit by an enemy.
 
the function u made for me is the enemy function so can i just add the above to the bottom?
 
Right..

Code:
numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
    mcEnemy = this.attachMovie("enemy", "enemy"+i, 100+i);
    reset(mcEnemy);
    mcEnemy.onEnterFrame = moveEnemy;
}
function reset(mc:MovieClip):Void {
    mc._x = 300;
    mc._y = Math.floor((Math.random()*200+100));
    mc.enemySpeed = Math.floor(Math.random()*4+1);
}
function moveEnemy():Void {
    if (tank_mc.scrollStart) {
        this._x -= this.enemySpeed+mainGround.groundSpeed;
    } else {
        this._x -= this.enemySpeed;
    }
    if (this._x<-10) {
        reset(this);
	if (this.hitTest(tank_mc)) {
    hpAmount--;
}
    }

Thats my full function now, but the health bar in the game just reads NaN and fails to do anything.
 
Do you get the correct trace?
Code:
[b]var hpAmount = 100;[/b]
var numEnemy = 8;
for (i=1; i<=numEnemy; i++) {
	mcEnemy = this.attachMovie("enemy", "enemy"+i, 100+i);
	reset(mcEnemy);
	mcEnemy.onEnterFrame = moveEnemy;
}
function reset(mc:MovieClip):Void {
	mc._x = 300;
	mc._y = Math.floor((Math.random()*200+100));
	mc.enemySpeed = Math.floor(Math.random()*4+1);
}
function moveEnemy():Void {
	if (tank_mc.scrollStart) {
		this._x -= this.enemySpeed+mainGround.groundSpeed;
	} else {
		this._x -= this.enemySpeed;
	}
	if (this._x<-10) {
		reset(this);
	}
	if (this.hitTest(tank_mc)) {
		hpAmount--;
		[b]trace("hpAmount: "+hpAmount);[/b]
	}
}

Kenneth Kawamoto
 
I just got 4 errors saying :


**Error** Scene=Scene 1, layer=Health, frame=1:Line 1: Statement must appear within on/onClipEvent handler
var hpAmount = 100;

**Error** Scene=Scene 1, layer=Health, frame=1:Line 2: Statement must appear within on/onClipEvent handler
var numEnemy = 8;

**Error** Scene=Scene 1, layer=Health, frame=1:Line 3: Statement must appear within on/onClipEvent handler
for (i=1; i<=numEnemy; i++) {

**Error** Scene=Scene 1, layer=Health, frame=1:Line 8: Statement must appear within on/onClipEvent handler
function reset(mc:MovieClip):Void {

**Error** Scene=Scene 1, layer=Health, frame=1:Line 13: Statement must appear within on/onClipEvent handler
function moveEnemy():Void {

Total ActionScript Errors: 5 Reported Errors: 5

 
Ah i can believe i did that again.

Right now its the trace window that opens and gives the reports. Can i make it so instead its the health bar that goes down and when it gets to 0 it says game over?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top