开发者

Enemy Inactive Until x pixels away from Hero

I'm using cocos2d and need help implementing a method. I'd like to have an enemy character remain inactive until he is a certain amount of pixels away from the Hero character. How would I do this? By inactive, I want him to stop all methods and basically pause all actions, UNTIL he is say 300 pixels away from the Hero character. I've thought about doing this through an if method, but just need some help.

Obviously, this wouldn't work, but it's an example of what I'd like to do.

-(void)enemyInactive:(id)sender {
if (enemy.position &开发者_开发知识库gt; ccp(600, 0)) { //if farther than 600 px away from Hero (many faults)
    //stop all enemy actions
} else if (mole.position < ccp(599, 0)) { //if less than 600 px away from Hero
    //resume all enemy actions
}

}


I believe you still remember Pythagoras' theorem.. A(sq)+B(sq) = C(sq);

so in code will be..

-(void)tick:(ccTime)delay
{
float x = enemy.position.x - hero.position.x;
float y = enemy.position.y - hero.position.y;
float xy = x*x + y*y;
if(xy<360000)
{
//resume actions..
}
else
{
//stop all
}
}

I recommend to call this per tick or something.. 360000 is square of 600.. Note that i am not using square root as it will consume some processing power when this function is called per tick..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜