Stick Page Forums Archive

Removing Movieclip (top down flash game)

Started by: Mr.Pancake | Replies: 10 | Views: 1,185

Mr.Pancake

Posts: 780
Joined: Jul 2008
Rep: 10

View Profile
Nov 3, 2008 5:22 PM #286054
Ok so i made this little top down thing, you can pick up the spike, slash with it with your mouse button. and it also goes into your inventory. but i can't get the remove movieclip to work if you have picked it up.


anyone have a solution?
http://www.jg-designs.nl/testing.fla
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Nov 3, 2008 6:09 PM #286087
I don't have flash on this computer, so I can't view your script. I can make some guesses, though.

If you're trying to use removeMovieClip(), it will only work for movieClips created with duplicateMovieClip(), attachMovie(), or createEmptyMovieClip(). So, if you placed the spear on the stage within Flash itself, it technically can't be deleted.

So you have two options, from what I can tell:
1) Spawn the spear at runtime using one of the above methods and then delete it using removeMovieClip()
2) Just hide the spear by setting _visible to false. (and be sure to check that _visible is not false before letting them pick something up)

Clearly, option 2 is easier to implement, while option 1 is more robust.


If this seems like a completely random response, posting the actionscript your question pertains to may be helpful :P
Mr.Pancake

Posts: 780
Joined: Jul 2008
Rep: 10

View Profile
Nov 3, 2008 7:06 PM #286107
Thanks I searched some stuff on the internet, and indeed i had to attach it first instead of placing the movieclip on stage :)

thanks for helping, nice signature to.

you seem pretty good at actionscript :D


do you, maybe, know how i move movieclip1 with a certain speed towards movieclip 2?

(i want to use it for the zombies moveing towards the player)
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Nov 3, 2008 8:24 PM #286124
Quote from Mr.Pancake
Thanks I searched some stuff on the internet, and indeed i had to attach it first instead of placing the movieclip on stage :)

thanks for helping, nice signature to.

you seem pretty good at actionscript :D


No problem, and thanks.

Quote from Mr.Pancake
do you, maybe, know how i move movieclip1 with a certain speed towards movieclip 2?

(i want to use it for the zombies moveing towards the player)


The best way I know of for moving a movieClip towards another is by using Trig. There is a simpler way, where you compare the differences in their locations and move their _x/_y static amounts, but it looks blocky and is confined to 8-direction movement. Using Trig allow for the "zombie", in this case, to move in 360+ directions, and turn to face the player.

If you've covered trigonometry in your Math class, this should be fairly simple once you see how it applies to movement. If you haven't, you may want to google "basic trigonometry" and read up a little. Or, I guess you could just nod-and-smile and use it anyways ;)

The three primary functions of Trig are sine, cosine, and tangent (written as sin(), cos(), and tan(); respectively). These functions are based on a right-triangle, which for our purposes, will be inscribed in a unit circle (circle with a radius of 1).

Image

cosine of some angle, Θ (pronounced "theta"), is the length of the horizontal leg of the right triangle; and sine of Θ (which is the variable used to represent angles) is the length of the vertical leg of the triangle. So, if we can create a triangle with a hypotenuse pointing towards the other movieClip, the width/height of its legs will be the distances the movieClip needs to move in the _x/_y directions to move closer to it. By changing the length of the hypotenuse itself, we can determine how much of a step it is taking.

So:
sin(Θ)=change in _x
cos(Θ)=change in _y
length of hypotenuse=magnitude/speed of change

But, how do we find Θ? By using an inverse trig function, where you pass the x/y values to get theta, as opposed to passing theta to get x/y. Inverse trig functions are represented by a "-1" after their name, or by prefixing them with "arc".

This is where tangent comes in. tan(Θ) is equal to y/x, so doing arctan(y/x) would equal Θ.

I have to go to class in a few more minutes, so here's where you end up:

//On the Zombie's Actions panel, inside it's onEnterFrame handler
onClipEvent(enterFrame) {
var Theta:Number = Math.arctan((_root.Player._y-this._y)/(_root.Player._x-this._x));
var Speed:Number = 7;
var cx:Number=Math.cos(Theta)*Speed;
var cy:Number=Math.sin(Theta)*Speed;
_x+=cx;
_y+=cy;
}


I'm pretty sure that's it, but I don't have access to Flash and I'm in a bit of a hurry. If it seems flipped in the vertical direction, try flipping "_root.Player._y-this._y" in the Theta calculation.

I'll be back in 3 hours if it doesn't work.
Mr.Pancake

Posts: 780
Joined: Jul 2008
Rep: 10

View Profile
Nov 3, 2008 8:55 PM #286136
Wow thanks :D

ill read trough it, i had some basic trigonometry at school but i forgot half of it :D

ill try it later, have to attend some other buisness D:
Wtf
2

Posts: 5,683
Joined: Oct 2006
Rep: 10

View Profile
Nov 3, 2008 9:13 PM #286149
trigonometry ftw.
I love it so much.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
Nov 3, 2008 9:14 PM #286151
I use this simple bit of code

-----
if(movie_clip1._x < _root.movieclip2._x){
movie_clip1._x ++;
}
if(movie_clip1._x >_root.movieclip2._x){
movie_clip1._x --;
}
----

You just repeat it for the x axis, and vola.

But watch out as when they are moving diagonally they will move a lot faster thanks to right angled trig.
Mr.Pancake

Posts: 780
Joined: Jul 2008
Rep: 10

View Profile
Nov 3, 2008 10:41 PM #286199
ok i tried some stuff, darkcampaigner yours didnt work because there is no method like arctan , i tried to change it to atan or any other methods, but they turned out wonky.

so i used the simpler but more buggy method:

http://www.jg-designs.nl/testing.swf
darkcampainger
2

Posts: 159
Joined: Aug 2006
Rep: 10

View Profile
Nov 3, 2008 11:18 PM #286233
Ah, I knew I would forget something!

Anyways, now that I'm back home, I put together a little demo for you:

Trig Movement Example.fla

It's commented and whatnot, but if you have any questions about it, I'd be happy to elaborate.

Here's what it produces:

[swf=http://www.zombiecannon.com/foo/TrigMovementExample.swf]width=550 height=400[/swf]
Mr.Pancake

Posts: 780
Joined: Jul 2008
Rep: 10

View Profile
Nov 4, 2008 9:22 AM #286520
Thanks dark,

I'm using the simpler code as it's better for me to understand, i'll keep your code for further studies.
i did use the rotation code tho.

now for another question :P

so i want the zombies to stop at like 20 pixels before hitting the player, i tried

if(_root.dy-20 <= this._y)

in front of the moving towards enemy code. but that didn't quite work :P

any pointers?

onClipEvent(enterFrame){
if(this.zombiehit2.hitTest(_root.hero.spikesla.spikein.hit)){
this.play();
_root.notdead=2;


}

}
onClipEvent(enterFrame){
var dx:Number=_root.hero._x-this._x; //Find the horizontal distance from the Chaser to the target
var dy:Number=_root.hero._y-this._y;
if(_root.notdead == 1){
if(_root.dy-20 <= this._y)
if(this._y < _root.hero._y and(_root.notdead ==1) ){
this._y +=3;

}else{
this._y-=3;
}
}
}

onClipEvent(enterFrame){
if(_root.notdead == 1){
if(this._x < _root.hero._x ){
this._x +=3;

}else{
this._x-=3;
}
}
if(_root.notdead == 1){

var Theta:Number = Math.atan2(dy,dx);
_rotation=180*Theta/Math.PI-90;
}
}
sticky505

Posts: 34
Joined: Sep 2008
Rep: 10

View Profile
Nov 4, 2008 9:39 PM #286833
hey darcampaigner ur movie aint loaded so i cant see it..would u mind if u could put it up agen?
thanks
Website Version: 1.0.4
© 2025 Max Games. All rights reserved.