您当前的位置: 首页 >  游戏

(NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)

发布时间:2015-11-11 10:05:35 ,浏览量:0

回到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的发射子弹的逻辑是一样的.

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    109276博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.2213s