开发者

hitesting the weapons

开发者 https://www.devze.com 2023-03-30 17:39 出处:网络
i have a movieclip on my stage called dude inside the moviclip is a frame with a movie clip called axeframe with yet another movie clip 开发者_JAVA百科called axe. what i want to do is make a hittest i

i have a movieclip on my stage called dude inside the moviclip is a frame with a movie clip called axeframe with yet another movie clip 开发者_JAVA百科called axe. what i want to do is make a hittest in the axeframe a so that when the axe (only axe not the character) hits an enemy(named enemy) on the stage he will disappear. this is my code:

addEventListener(Event.ENTER_FRAME, axehit);

function axehit(event:Event):void {

if (axe.hitTestObject(enemy)) {
 removeChild(enemy.stage)
}
}

it gives me this error

1120: Access of undefined property enemy.if (axe.hitTestObject(enemy)) {

1120: Access of undefined property enemy.removeChild(enemy.stage)


You can't just reference to enemy without any further specification (it will assume enemy is a child of the movieclip in which you placed the code. Try stage.enemy instead, and this, or this.parent instead of axe. (Assuming enemy is a movieclip on the stage, and the code you posted is inside axe) Also, you should change removeChild(enemy.stage) to stage.removeChild(stage.enemy), and you should probably look into variable scopes.


Edit: Nope. Sorry for that, just pretend you didn't read that (Forgot you can't just reference objects through Stage)

To be completely honest with you, this is the way I started as well but it's not the proper approach to flash coding. Firstly, you should try to keep all your code on the main timeline, and not in separate movieclips, in order for it to work together better. Once you get the hang of it, you should check out Object Oriented programming as well. It really increases the workflow and enables you to create bigger and way more complicated scripts.

more edits:

So to put this on the main timeline, would require something such as:

stage.addEventListener(Event.ENTER_FRAME, loop);

function loop(event:Event):void {
    if (axe.hitTestObject(enemy)) {
        this.removeChild(enemy)
    }
}

Which is cleaner and more readable (and easier to find) as well. (Assuming axe and enemy are movieclips on the main stage)

0

精彩评论

暂无评论...
验证码 换一张
取 消