Cocos2D实现RPG队伍菜单随意调整角色顺序的效果

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! hopy ;)


前一篇我们实现了队伍实现拖尾效果,可是在实际游戏中我们往往须要依据不同的角色能力,角色职业对角色队伍进行排序,并且希望排序后的效果能马上在游戏地图场景中反应出来,所以在本篇博文中我们就来看看怎样实现调整角色顺序并且马上在地图中显示的功能.

这里写图片描写叙述

能够看到我们在将角色Panda从队列头部移至第3位后,地图上角色队列也马上发生了改变.

首先打开SpriteBuilder,将界面调整例如以下:

这里写图片描写叙述

注意左側竖排4个button每一个上面都有一个相应的sprite和label对象.

为了实如今游戏界面上任务的拖拽,我们有非常多方法,我这里使用了UIKit中UIButton能够加入手势的特点.在每一个UIButton中加入一个长按手势.可是CCButton默认是不支持手势加入的,所以我们必须动态加入4个UIButtonbutton,So,Let’s Do it!!!

在Xcode中加入initUIBtns方法:

-(void)initUIBtns{
    UIButton *uiBtn;
    CCButton *btn;
    for (int i = 0; i < _btns.count; i++) {
        uiBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn = _btns[i];
        CGRect rect = btn.boundingBox;
        CGPoint pos = [_peopleLayoutBox convertToWindowSpace:rect.origin];
        pos.y -= btn.boundingBox.size.height;
        rect.origin = pos;
        uiBtn.frame = rect;
        [[CCDirector sharedDirector].view addSubview:uiBtn];
        [_uiBtns addObject:uiBtn];

        [uiBtn addTarget:self action:@selector(uiBtnCallback:) forControlEvents:UIControlEventTouchDown];
        uiBtn.tag = i;

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(btnLongPress:)];
        longPress.minimumPressDuration = 0.8;
        [uiBtn addGestureRecognizer:longPress];
    }
}

代码主要功能例如以下:
1.依次读取每一个CCButton的位置
2.在该位置处新建一个”透明”的UIButton
3.给该button加入手势识别器和press回调方法

在press回调方法中手动调用原CCButton的回调方法,这个比較简单,然后在手势识别器长按的回调中处理角色sprite的拖拽功能:

-(void)btnLongPress:(UILongPressGestureRecognizer*)gr{
    CGPoint location = [gr locationInView:[CCDirector sharedDirector].view];
    UIButton *uiBtn = (UIButton*)gr.view;
    NSInteger idx = uiBtn.tag;
    CCSprite *bg = _bgs[idx];

    CCSprite *bgNode = [bg children][0];
    bgNode.opacity = 0.5;
    if (gr.state == UIGestureRecognizerStateBegan) {
        CCLOG(@"%@ began!!!",NSStringFromSelector(_cmd));
    }else if (gr.state == UIGestureRecognizerStateEnded) {
        CCLOG(@"%@ ended!!!",NSStringFromSelector(_cmd));
        NSValue *v = _bgPositions[idx];
        NSInteger otherIdx = [self getBgInside2:bg.position];
        if (otherIdx == 0 || otherIdx == idx + 1) {

            bg.position = v.CGPointValue;

        }else{
            CCLOG(@"otherIdx is %ld",(long)otherIdx);
            CCSprite *otherBg = _bgs[otherIdx-1];
            GameCharacter *otherGC = [otherBg children][0];
            GameCharacter *gc = [bg children][0];
            [otherGC removeFromParentAndCleanup:NO];
            [gc removeFromParentAndCleanup:NO];
            [otherBg addChild:gc];
            [bg addChild:otherGC];
            bg.position = v.CGPointValue;
            GameData *gd = [GameData sharedInstance];
            [gd switchPlayerAtIndex:idx andAtIndex:otherIdx-1];
            [self switchLabelStringAtIndex:idx andIndex:otherIdx-1];
        }
        bgNode.opacity = 1.0f;
    }else{
        location = [[CCDirector sharedDirector] convertToGL:location];
        location = [self convertToNodeSpace:location];
        CGRect boundary = self.boundingBox;
        if (CGRectContainsPoint(boundary, location)) {
            bg.position = location;
        }else{
            NSValue *v = _bgPositions[idx];
            bg.position = v.CGPointValue;
            bgNode.opacity = 1.0f;
        }
    } 
}

以上代码首先转换坐标类型,然后找到长按相应的button,从而取得相应的sprite.接着在触摸拖动时推断是否移出了当前界面的范围,假设是则sprite归位,否则sprite尾随触摸一起移动.最后当长按完毕时推断是否发生sprite之间的交换,假设是则完毕交换功能,否则sprite相同回到原来的位置.

在交换功能中,我们相同要改动内部的数据结构已完毕数据的改动,就像MVC机制一样.最后在界面层关闭时,我们又一次给游戏队伍在大地图上排序,由于你可能改动了队伍的顺序,所以我们也要改动地图中显示的顺序.

这里写图片描写叙述

这样一个重排序的功能就完毕了,是不是非常有成就感呢 ;)

posted @ 2017-06-16 20:31  gccbuaa  阅读(231)  评论(0编辑  收藏  举报