回到MainScene.m中添加selectRobot方法:
-(void)selectRobot:(Robot *)robot{
LevelRestrict *lr = [LevelRestrict sharedInstance];
for (Robot *r in lr.robotsInLevel) {
if (r != robot) {
r.isSelected = NO;
}
}
}
代码逻辑已经说过了,将LevelRestrict中机器人数组修改为正确的状态:反选其他所有的机器人.
添加MainScene的触摸回调已支持用户在屏幕上触摸,我们一个一个方法看,首先是touchBegan方法:
-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
CCLOG(@"%@",NSStringFromSelector(_cmd));
_touchPoint = [[CCDirector sharedDirector] convertTouchToGL:touch];
}
这个和Arm的类似,下面是touchMoved方法:
-(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch];
LevelRestrict *lr = [LevelRestrict sharedInstance];
//如果当前选中了一个robot
Robot *robot = lr.selectedRobot;
if (robot) {
MoveDirection direction = armMoveDirectionDown;
if (location.y > _touchPoint.y) {
direction = armMoveDirectionUp;
}else if (location.y < _touchPoint.y){
direction = armMoveDirectionDown;
}
[robot moveArm:direction];
}
}
该方法主要逻辑为,如果当前选中了一个机器人则取得其手臂需要转动的方向,然后给robot发送moveArm消息以转动手臂.
最后是touchEnd方法:
-(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
LevelRestrict *lr = [LevelRestrict sharedInstance];
Robot *robot = lr.selectedRobot;
if (robot) {
[robot armShoot];
}
}
在玩家接触触摸时发射子弹.这个和Arm的发射子弹的逻辑是一样的.