写到这突然有童鞋质疑,你这哪里是牛,分明是熊嘛!
仔细看了下,还真像牛.反正是这个意思.怪本猫猪牛熊不分,好在道理是一样的.
下面继续,言归正传.
添加一个空白的touchBegan方法,如果没有这个方法,其他触摸回调也不会被响应.
接着是重点部分来了:
-(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch];
location = [self convertToNodeSpace:location];
CGSize viewSize = [CCDirector sharedDirector].viewSize;
float bearVelocity = viewSize.width/3.0;
CGPoint diff = ccpSub(location, _bear.position);
float distance = ccpLength(diff);
float moveDuration = distance/bearVelocity;
//根据玩家点击的位置设置熊头的方向
if (diff.x < 0) {
_bear.flipX = NO;
}else{
_bear.flipX = YES;
}
[_bear stopAction:_moveAction];
if (!_bearMoving) {
[_bear runAction:_walkAction];
}
CCActionMoveTo *move = [CCActionMoveTo actionWithDuration:moveDuration position:location];
CCActionCallBlock *block = [CCActionCallBlock actionWithBlock:^{
[_bear stopAction:_walkAction];
_bearMoving = NO;
}];
CCActionSequence *seq = [CCActionSequence actions:move,block,nil];
_moveAction = seq;
[_bear runAction:_moveAction];
_bearMoving = YES;
}
原文中seq行为最后用来回调方法,这里直接用回调block即可,不用再另外写一个方法了.
代码没啥好说的,主要是计算实际移动时间,是否翻转熊头,然后根据熊的状态确定是否播放动画等行为.
在编译执行之前,现将didLoadFromCCB方法中的:
[_bear runAction:_walkAction];
一行注释掉,否则会发生重复播放动画的错误.
现在编译运行app,效果如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-23 23:35:59