Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2D实现RPG队伍菜单任意调整角色顺序的效果大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

注意左侧竖排4个按钮每个上面都有一个对应的sprite和label对象.

为了实现在游戏界面上任务的拖拽,我们有很多方法,我这里使用了UIKit中UIButton可以添加手势的特点.在每个UIButton中添加一个长按手势.但是CCButton默认是不支持手势添加的,所以我们必须动态添加4个UIButton按钮,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.给该按钮添加手势识别器和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;
        }
    } 
}

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

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

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

大佬总结

以上是大佬教程为你收集整理的Cocos2D实现RPG队伍菜单任意调整角色顺序的效果全部内容,希望文章能够帮你解决Cocos2D实现RPG队伍菜单任意调整角色顺序的效果所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。