侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

cocos2d_基础篇1_点击让一个精灵作动作

2022-06-14 星期二 / 0 评论 / 0 点赞 / 115 阅读 / 2930 字

开始写正式一点的分享了!想说的是游戏开发离不开点击、触碰、挤压等等的人为动作来增强游戏的可玩性!(还请大牛们多多指点!) 现在就动手,demo的目的是点击一个精灵,然后让它做你指定的动作,譬如旋转

开始写正式一点的分享了!想说的是游戏开发离不开点击、触碰、挤压等等的人为动作来增强游戏的可玩性!(还请大牛们多多指点!)

现在就动手,demo的目的是点击一个精灵,然后让它做你指定的动作,譬如旋转、跳跃、消失等等!我就弄个旋转的吧!你们自己据一方三!

1.打开xcode,新建项目,命名随意!哇卡卡卡!我就交TouchDemo!

之后你就看到文件目录就是这样的:

但是我之前一开始想说再introLayer.h/m 的“-(void) onEnter”里面写实现代码!but,shit!代码木有错,精灵也正常显示了!重点就是不做动作!后来我去到helloWorldLayer 里面写实现代码就OK!有时间再研究为神马哈哈!接着猪蹄!

2.打开HelloWorldLayer.h文件,声明一个精灵,和一个背景:

CCSprite *MySprite; CCSprite *background;
然后在init方法里添加代码如下:

if( (self=[super init]) ) {				// create and initialize a Label        CGSize winSize=[CCDirector sharedDirector].winSize;        //告诉cocos2d使用RBG565的像素格式        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];        background = [CCSprite spriteWithFile:@"sexLady.png"];        background.anchorPoint = ccp(0,0);        [self addChild:background];        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];                MySprite = [CCSprite spriteWithFile:@"sprite.png"];        float offsetFraction = ((float)(1+1))/(4+1);        MySprite.position = ccp(winSize.width*offsetFraction, winSize.height/2);        [self addChild:MySprite];        //这个是启动ccTouch delegate事件         [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];	}
然后呢写一个你要实现动作的方法:

- (void)SpriteForTouch:(CGPoint)touchLocation {             if (CGRectContainsPoint(MySprite.boundingBox, touchLocation))     {                CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:2 angle:360];          CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy];          [MySprite runAction:repeat];            }         }
最后调用ccTouchBegan方法:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {        CGPoint touchLocation = [self convertTouchToNodeSpace:touch];    [self SpriteForTouch:touchLocation];    return TRUE;}
so this is the end of demo and run it and have fun!!!!see u!!!

广告 广告

评论区