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

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

cocos2d-iphone第二课  

2013-05-24 20:32:47|  分类: ios游戏开发 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
利用随机数创建精灵的随机位置,以及单击精灵事件的处理,代码如下:

@implementation HelloWorldLayer

@synthesize bird;


// Helper class method that creates a Scene with the HelloWorldLayer as the only child.

+(CCScene *) scene

{

// 'scene' is an autorelease object.

CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.

HelloWorldLayer *layer = [HelloWorldLayer node];

// add layer as a child to scene

[scene addChild: layer];

// return the scene

return scene;

}


- (void) animated_finished

{

    int x = arc4random() % 480;

    int y = arc4random() % 320;

    

    id moveToAction = [CCMoveTo actionWithDuration:2.0 position:ccp(x,y)];

    id callback = [CCCallFunc actionWithTarget:self selector:@selector(animated_finished)];

    

    [self.bird runAction:[CCSequence actions:moveToAction,callback,nil]];

}


// on "init" you need to initialize your instance

-(id) init

{

// always call "super" init

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

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

        

        isTouchEnabled_ = YES;

        self.bird = [CCSprite spriteWithFile:@"Icon-72.png"];

        

        int x = arc4random() % 320;

        int y = arc4random() % 480;

        

        id moveToAction = [CCMoveTo actionWithDuration:2.0 position:ccp(x,y)];

        id callback = [CCCallFunc actionWithTarget:self selector:@selector(animated_finished)];

        

        [self.bird runAction:[CCSequence actions:moveToAction,callback,nil]];

        

        [self addChild:self.bird];

}

return self;

}


// on "dealloc" you need to release all your retained objects

- (void) dealloc

{

// in case you have something to dealloc, do it in this method

// in this particular example nothing needs to be released.

// cocos2d will automatically release all the children (Label)

// don't forget to call "super dealloc"

[super dealloc];

}


#pragma mark Touches

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touches anyObject];

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

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

    

    float distance = powf(self.bird.position.x - location.x,2) + powf(self.bird.position.y - location.y, 2);

    distance = sqrtf(distance);

    

    if (distance <= 25)

    {

        [self.bird runAction:[CCRotateBy actionWithDuration:0.9 angle:360]];

    }

}


#pragma mark GameKit delegate


-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController

{

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

[[app navController] dismissModalViewControllerAnimated:YES];

}


-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{

AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];

[[app navController] dismissModalViewControllerAnimated:YES];

}

@end


在头文件中申请bird,代码如下:

@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>

{

    CCSprite *bird;

}


// returns a CCScene that contains the HelloWorldLayer as the only child

+(CCScene *) scene;


@property (nonatomic,retain) CCSprite *bird;


@end


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

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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