注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

cocos2d-创建一个可以移动的精灵(触摸事件的应用)  

2013-06-05 23:24:35|  分类: ios游戏开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#import <Foundation/Foundation.h>

#import "cocos2d.h"


@interface KillingLight : CCSprite <CCTargetedTouchDelegate>{

    

}


+ (id) KillingLightWithRect: (CGRect) rect spriteManager: (CCSpriteBatchNode*) manager;


@end


-----------------------------------------------------------

#import "KillingLight.h"



@implementation KillingLight


+ (id) KillingLightWithRect:(CGRect)rect spriteManager:(CCSpriteBatchNode *)manager

{

    id rtn = [[[self alloc] initWithTexture:manager.texture rect:rect] autorelease];

    

    return rtn;

}


- (CGRect) rect

{

    return CGRectMake(-rect_.size.width/2, -rect_.size.height/2, rect_.size.width, rect_.size.height);

}


- (void) onEnter

{

    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

    [super onEnter];

}


- (void) onExit

{

    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];

    [super onExit];

}


- (BOOL) containsTouchLocation: (UITouch*) touch

{

    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);

}


- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

{

    if (![self containsTouchLocation:touch])

        return NO;

    return YES;

}


- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event

{

    CGPoint touchPoint = [touch locationInView:[touch view]];

    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    

    self.position = CGPointMake(touchPoint.x, touchPoint.y);

}


- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

{

    

}


- (void) dealloc

{

    [super dealloc];

}


@end


主程序中调用,代码发下:

-(id) init

{

// always call "super" init

// Apple recommends to re-assign "self" with the "super's" return value

if( (self=[super init]) ) {

        CCSpriteBatchNode *mgr = [CCSpriteBatchNode batchNodeWithFile:@"killer.png" capacity:10];

        [self addChild:mgr z:0];

        

        KillingLight *sprite = [KillingLight KillingLightWithRect:CGRectMake(8*32, 0, 32, 134) spriteManager:mgr];

        [mgr addChild:sprite z:1 tag:5];

        

        sprite.position = ccp(240,160);

        NSMutableArray *frames = [[NSMutableArray alloc] init];

        NSMutableArray *frames2 = [[NSMutableArray alloc] init];

        for (int i = 0 ; i < 9 ; i++)

        {

            CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake((8-i)*32, 0, 32, 134)];

            [frames addObject:frame];

        }

        CCAnimation *animation = [CCAnimation animationWithSpriteFrames:frames delay:0.05f];

        id action = [CCAnimate actionWithAnimation:animation];

        

        for (int i = 0 ; i < 9 ; i++)

        {

            CCSpriteFrame *frame2 = [CCSpriteFrame frameWithTexture:mgr.texture rect:CGRectMake(i*32, 0, 32, 134)];

            [frames2 addObject:frame2];

        }

        CCAnimation *animation2 = [CCAnimation animationWithSpriteFrames:frames2 delay:0.05f];

        id r_action = [CCAnimate actionWithAnimation:animation2];

        

        id ac = [CCSequence actions:[CCDelayTime actionWithDuration:0.3],action,r_action,nil];

        

        [sprite runAction:[CCRepeatForever actionWithAction:ac]];

}

return self;

}

  评论这张
 
阅读(74)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017