Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(六)

Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(六),第1张

概述大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在Xcode中打开MainScene.h文件,在接口中添加2个方法: -(void)winGame; -(void)loseGame; 回到MainScene.m文件中,转换对应的实现代码: -(void)endScene{ [_cat runAction

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)

在Xcode中打开MainScene.h文件,在接口中添加2个方法:

-(voID)winGame; -(voID)loseGame;

回到MainScene.m文件中,转换对应的实现代码:

-(voID)endScene{    [_cat runAction:[CCActionSequence actions:                     [CCActionScaleBy actionWithDuration:0.5 scale:3.0],[CCActionDelay actionWithDuration:1.0],[CCActionScaleto actionWithDuration:0.5 scale:0],[CCActionCallFunc actionWithTarget:self selector:@selector(showRestartmenu)],nil]];    [_cat runAction:[CCActionRepeatForever actionWithAction:                     [CCActionRotateBy actionWithDuration:0.5 angle:360]]];}-(voID)winGame{    _gameOver = YES;    _won = YES;    [self endScene];}-(voID)loseGame{    _gameOver = YES;    _won = NO;    [self endScene];}

winGame和loseGame方法最终都调用了endScene方法,而在endScene方法中实现了老鼠旋转缩放的动画效果,在动画结束时又调用了一个内部方法showRestartmenu.我们先来看一下这个方法在原代码里是如何实现的:

//原代码中的方法实现- (voID)showRestartmenu {    CGSize winSize = [CCDirector sharedDirector].winSize;    Nsstring *message;    if (_won) {        message = [Nsstring stringWithFormat:@"You win![left %d bones]",_cat.numBones];    } else {        message = @"You lose!";    }    cclabelBMFont *label;    if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) {        label = [cclabelBMFont labelWithString:message fntfile:@"Arial-hd.fnt"];    } else {        label = [cclabelBMFont labelWithString:message fntfile:@"Arial.fnt"];    }    label.scale = 0.1;    label.position = ccp(winSize.wIDth/2,winSize.height * 0.6);    [self addChild:label];    cclabelBMFont *restartLabel;    if (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad) {        restartLabel = [cclabelBMFont labelWithString:@"Restart" fntfile:@"Arial-hd.fnt"];        } else {        restartLabel = [cclabelBMFont labelWithString:@"Restart" fntfile:@"Arial.fnt"];        }    CcmenuItemLabel *restartItem = [CcmenuItemLabel itemWithLabel:restartLabel target:self selector:@selector(restartTapped:)];    restartItem.scale = 0.1;    restartItem.position = ccp(winSize.wIDth/2,winSize.height * 0.4);    Ccmenu *menu = [Ccmenu menuWithItems:restartItem,nil];    menu.position = CGPointZero;    [self addChild:menu z:10];    [restartItem runAction:[CCScaleto actionWithDuration:0.5 scale:1.0]];    [label runAction:[CCScaleto actionWithDuration:0.5 scale:1.0]];}

可以看到代码比较长,但功能很简单:就是根据游戏胜利条件打造一个显式界面.界面中有2个标签,第一个显示胜利或失败的消息,后一个当成一个按钮来用,如果用户点击它,则重新载入MainScene场景.

但是其中一些类在Cocos2Dv3.4中已经没有了,比如CcmenuItemLabel.如果用CCbutton类代替的话,则正常情况下Cocos2Dv3.4中的按钮类CCbutton里的标签是不可以设置为cclabelBMFont类型的.

那么我们怎么达到原代码中的效果呢?答案是用SpriteBuilder强大和方便的Layer界面创建能力来搞定!我们将在下一篇中详述创建过程.see you ;)

总结

以上是内存溢出为你收集整理的Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(六)全部内容,希望文章能够帮你解决Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(六)所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1074964.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存