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

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

现在就动手,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!!!

转载于:https://my.oschina.net/dadas/blog/90455

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值