`

Cocos2D Animation Sprites

 
阅读更多
#import "cocos2d.h"
#import "Recipe.h"
#import "SimpleAnimObject.h"

enum {
	TAG_CASTLE = 0,
	TAG_LIGHTNING_BOLT = 1,
	TAG_BATS = 2
};

//Bat animation types
enum {
	BAT_FLYING_UP = 0,
	BAT_GLIDING_DOWN = 1
};

//Interface
@interface Ch1_AnimatingSprites : Recipe
{
	NSMutableArray *bats;
	CCAnimation *batFlyUp;
	CCAnimation *batGlideDown;
	CCSprite *lightningBolt;
	CCSprite *lightningGlow;
	int lightningRemoveCount;
}

-(CCLayer*) runRecipe;
-(float)makeBatFlyUp:(SimpleAnimObject*)bat;
-(void)makeBatGlideDown:(SimpleAnimObject*)bat;

@end

//Implementation
@implementation Ch1_AnimatingSprites

-(CCLayer*) runRecipe {
	//Add our PLIST to the SpriteFrameCache
	[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"simple_bat.plist"];
	
	//Add castle background
	CCSprite *castle = [CCSprite spriteWithFile:@"dracula_castle.jpg"];
	[castle setPosition:ccp(240,160)];
	[self addChild: castle z:0 tag:TAG_CASTLE];
	
	//Add a lightning bolt
	lightningBolt = [CCSprite spriteWithFile:@"lightning_bolt.png"];
	[lightningBolt setPosition:ccp(240,160)];
	[lightningBolt setOpacity:64];
	[lightningBolt retain];

	//Add a sprite to make it light up other areas.
	lightningGlow = [CCSprite spriteWithFile:@"lightning_glow.png"];
	[lightningGlow setColor:ccc3(255,255,0)];
	[lightningGlow setPosition:ccp(240,160)];
	[lightningGlow setOpacity:100];
	[lightningGlow setBlendFunc: (ccBlendFunc) { GL_ONE, GL_ONE }];
	[lightningBolt addChild:lightningGlow];
	
	//Set a counter for lightning duration randomization
	lightningRemoveCount = 0;

	//Bats Array Initialization
	bats = [[NSMutableArray alloc] init];
	
	//Add bats using a batch node.
	CCSpriteBatchNode *batch1 = [CCSpriteBatchNode batchNodeWithFile:@"simple_bat.png" capacity:10];
	[self addChild:batch1 z:2 tag:TAG_BATS];
	
	//Make them start flying up.
	for(int x=0; x<10; x++){
		//Create SimpleAnimObject of bat
		SimpleAnimObject *bat = [SimpleAnimObject spriteWithBatchNode:batch1 rect:CGRectMake(0,0,48,48)];
		[batch1 addChild:bat];
		[bat setPosition:ccp(arc4random()%400+40, arc4random()%150+150)];
		
		//Make the bat fly up. Get the animation delay (flappingSpeed).
		float flappingSpeed = [self makeBatFlyUp:bat];
		
		//Base y velocity on flappingSpeed.
		bat.velocity = ccp((arc4random()%1000)/500 + 0.2f, 0.1f/flappingSpeed);
		
		//Add a pointer to this bat object to the NSMutableArray
		[bats addObject:[NSValue valueWithPointer:bat]];
		[bat retain];
		
		//Set the bat's direction based on x velocity.
		if(bat.velocity.x > 0){
			bat.flipX = YES;
		}
	}

	//Schedule physics updates
	[self schedule:@selector(step:)];
	
	return self;
}

-(float)makeBatFlyUp:(SimpleAnimObject*)bat {
	CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];

	//Randomize animation speed.
	float delay = (float)(arc4random()%5+5)/80;
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"simply_bat_fly" delay:delay];

	//Randomize animation frame order.
	int num = arc4random()%4+1;
	for(int i=1; i<=4; i+=1){
		[animation addFrame:[cache spriteFrameByName:[NSString stringWithFormat:@"simple_bat_0%i.png",num]]];
		num++;
		if(num > 4){ num = 1; }
	}		
	
	//Stop any running animations and apply this one.
	[bat stopAllActions];
	[bat runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation]]];
	
	//Keep track of which animation is running.
	bat.animationType = BAT_FLYING_UP;

	return delay;	//We return how fast the bat is flapping.
}

-(void)makeBatGlideDown:(SimpleAnimObject*)bat {
	CCSpriteFrameCache * cache = [CCSpriteFrameCache sharedSpriteFrameCache];

	//Apply a simple single frame gliding animation.
	CCAnimation *animation = [[CCAnimation alloc] initWithName:@"simple_bat_glide" delay:100.0f];
	[animation addFrame:[cache spriteFrameByName:@"simple_bat_01.png"]];
	
	//Stop any running animations and apply this one.
	[bat stopAllActions];
	[bat runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation]]];
	
	//Keep track of which animation is running.
	bat.animationType = BAT_GLIDING_DOWN;
}

-(void)step:(ccTime)delta {
	CGSize s = [[CCDirector sharedDirector] winSize];

	for(id key in bats){
		//Get SimpleAnimObject out of NSArray of NSValue objects.
		SimpleAnimObject *bat = [key pointerValue];
	
		//Make sure bats don't fly off the screen
		if(bat.position.x > s.width){
			bat.velocity = ccp(-bat.velocity.x, bat.velocity.y);
			bat.flipX = NO;
		}else if(bat.position.x < 0){
			bat.velocity = ccp(-bat.velocity.x, bat.velocity.y);
			bat.flipX = YES;
		}else if(bat.position.y > s.height){
			bat.velocity = ccp(bat.velocity.x, -bat.velocity.y);
			[self makeBatGlideDown:bat];
		}else if(bat.position.y < 0){
			bat.velocity = ccp(bat.velocity.x, -bat.velocity.y);
			[self makeBatFlyUp:bat];
		}
		
		//Randomly make them fly back up
		if(arc4random()%100 == 7){
			if(bat.animationType == BAT_GLIDING_DOWN){ [self makeBatFlyUp:bat]; bat.velocity = ccp(bat.velocity.x, -bat.velocity.y); }
			else if(bat.animationType == BAT_FLYING_UP){ [self makeBatGlideDown:bat]; bat.velocity = ccp(bat.velocity.x, -bat.velocity.y); }
		}
		
		//Update bat position based on direction
		bat.position = ccp(bat.position.x + bat.velocity.x, bat.position.y + bat.velocity.y);
	}
	
	//Randomly make lightning strike
	if(arc4random()%70 == 7){
		if(lightningRemoveCount < 0){
			[self addChild:lightningBolt z:1 tag:TAG_LIGHTNING_BOLT];
			lightningRemoveCount = arc4random()%5+5;
		}
	}
	
	//Count down
	lightningRemoveCount -= 1;
	
	//Clean up any old lightning bolts
	if(lightningRemoveCount == 0){
		[self removeChildByTag:TAG_LIGHTNING_BOLT cleanup:NO];
	}
}

-(void) cleanRecipe {
	[bats release];
	[super cleanRecipe];
}

@end
分享到:
评论

相关推荐

    cocos2d-android-1资源:API文档

    Cocos2d是一个开源的游戏开发框架,被广泛用于创建2D游戏、演示程序和其他图形交互应用。在Android平台上,cocos2d-x是其主要实现,而“cocos2d-android-1”可能是该框架的一个特定版本。这个压缩包中的“cocos2d-...

    Rapid Game Development Using Cocos2d-JS(Apress,2016)

    You will dive deep into sprites, the most important entity in Cocos2d-JS, animation APIs, and primitive shapes. You’ll also learn about the Cocos2d-JS UI system to get a head start in 2d game ...

    cocos2d-x 3.2 物理小游戏教程 block it

    《cocos2d-x 3.2 物理小游戏教程 "Block It" 深度解析》 在游戏开发领域,cocos2d-x 是一个广泛使用的开源游戏引擎,尤其适用于2D游戏的开发。本教程将聚焦于利用cocos2d-x 3.2 版本制作一款名为 "Block It" 的物理...

    Cocos2D-iPhone开发教程(最全,最威的)

    3. **精灵(Sprites)**:精灵是Cocos2D-iPhone中的基本图形元素,可以用来显示静态或动态的图像。它们可以独立移动、旋转,也可以执行各种动作。 4. **物理引擎(Box2D)**:Cocos2D-iPhone集成了Box2D物理引擎,...

    知易Cocos2D-iPhone 游戏开发教程007

    1. **Cocos2D-iPhone基础**:了解Cocos2D-iPhone的核心架构,包括场景(Scenes)、层(Layers)、精灵(Sprites)和节点(Nodes)等基本元素,以及它们之间的关系和交互方式。 2. **粒子系统(Particle Systems)**...

    Cocos2D-X游戏开发技术精解学习

    接着,书中将深入讲解Cocos2D-X的核心组件,如场景(Scenes)、层(Layers)、节点(Nodes)、精灵(Sprites)等,这些都是构建游戏场景的基础元素。同时,还会涉及动作(Action)和动画(Animation)的创建,它们是赋予游戏动态...

    Bear cocos2d-x

    3. **精灵(Sprites)**:CCSprite是cocos2d-x中的基本图形元素,可以表示游戏中的角色、物品等。它们可以从精灵表中加载,并能进行移动、旋转、缩放等操作。 4. **时间轴(Timeline)**:cocos2d-x 3.x版本引入了...

    cocos2d-x打飞机小游戏源码

    1. 精灵(Sprites):cocos2d-x中的精灵是游戏画面的基本元素,可以是飞机、敌人或子弹。它们有自己的位置、大小、颜色等属性,以及显示的图像。 2. 动画:通过`cc.Animation`类,可以创建并播放精灵的动画,如飞机...

    英文原版-Rapid Game Development Using Cocos2dJS 1st Edition

    JS teaches you the overall architecture of Cocos2d-JS and explains the internal working of the framework.You will dive deep into sprites, the most important entity in Cocos2d-JS, animation APIs, and ...

    iOS游戏应用源代码——zootella-cocos2d-abfc8f6.zip

    《深入剖析iOS游戏应用源代码——以Zootella-Cocos2D为例》 在iOS开发领域,游戏应用占据着重要地位,而Cocos2D作为一款强大的2D游戏引擎,深受开发者喜爱。本文将深入探讨名为"Zootella-Cocos2D-abfc8f6"的iOS游戏...

    Cocos2d-X游戏源码 FlappyBird源码.7z

    2. **精灵与动画(Sprites and Animations)**:小鸟、管道等游戏元素在Cocos2d-X中表现为Sprite,它们可以被移动、旋转、缩放。源码中通过帧动画(Frame Animation)实现小鸟飞行和管道滚动的效果,这涉及到`...

    coco2D开发包

    3. **Sprites(精灵)**:精灵是Cocos2D中的基本2D图像元素,可以进行移动、旋转、缩放和颜色调整等操作。通过组合和动画化多个精灵,可以创建复杂的2D角色和效果。 4. **Animation(动画)**:Cocos2D提供了强大的...

    Cocco2D-iPhone开发教程part05

    在本教程中,我们将深入探讨Cocos2D-iPhone的开发,这是一款强大的2D游戏引擎,用于构建iOS平台上的游戏和其他互动应用。Cocos2D-iPhone是Cocos2D的一个分支,专为Apple的移动设备优化。在这个Part05中,我们将继续...

    Cocos Studio 创建帧动画

    Cocos2d-X是一个广泛使用的开源游戏引擎,而Cocos Studio作为其配套的可视化编辑工具,为开发者提供了一个方便、直观的方式来创建和管理帧动画。 在Cocos Studio中,帧动画的创建主要包括以下几个步骤: 1. **资源...

    Cocco2D-iPhone开发教程part04

    2. **精灵(Sprites)与动作(Actions)**:精灵是Cocos2D-iPhone中表示2D图像的基本元素,它们可以移动、旋转、缩放等。动作则是控制精灵行为的方式,包括平移、旋转、淡入淡出等效果。通过组合动作,开发者可以...

    一个c++基于cocos2dx开发的粒子系统源码.zip

    5. **动画与更新循环(Animation and Update Loop)**:在Cocos2d-x中,所有的动画都是在主循环中进行的,粒子系统类会在这个循环中调用每个粒子的更新方法。 6. **粒子精灵(Particle Sprites)**:粒子通常以精灵...

    IOS应用源码——zuwiki-RainbowDash2D-450e340.rar

    在iOS平台上,2D游戏通常使用SpriteKit或Cocos2D等框架进行开发,这两个都是Apple官方支持的游戏引擎。因此,我们可以预期这个项目可能涉及到SpriteKit或者Cocos2D的相关知识。 1. ** SpriteKit框架 **:如果项目...

    Sprite_Sheet_Animation:在cocos2dx for android平台使用精灵表创建动画

    CCTexture2D* texture = CCTextureCache::sharedTextureCache()-&gt;addImage("sprites.png"); // 获取精灵帧 CCSpriteFrameCache::sharedSpriteFrameCache()-&gt;addSpriteFramesWithFile("sprites.plist"); // 创建...

    kenney-simple-space:来自https的简单空间精灵

    如果是使用Unity、Cocos2d-x、Godot等游戏引擎,会有相应的API来加载精灵图像,并将其绑定到游戏对象上。例如,在Unity中,可以使用Sprite Renderer组件来显示精灵,并通过改变其Sprite属性来切换不同的图像。在...

Global site tag (gtag.js) - Google Analytics