This document provides information about an iOS DevCamp produced by CSDN. It includes the website and Weibo links for the event. The document then contains several sections about using Cocos2D, including its origins as a framework created to avoid "reinventing the wheel" for each game project. It discusses features of Cocos2D like its simplicity, large number of existing games built with it, and active community support.
4. “In the summer of 2008, several "pythoneros" went to a camp
program called PyCamp: Python outdoors. This camp
held in Cordoba, in the town of Los Cocos. We were about
50 people. It covered different topics, one of which was seeking
way of not having to "reinvent the wheel" in each jurisdiction
Pyweek.
In this way a group of friends and I decided to put together a
framework to make games. We call cocos2d, since the framework
is designed to make 2D games, and the word "coconut" emulates
the place where the camp took place.”
Ricardo Quesada
Thursday, August 2, 12
6. It is free!
Without having to learn OpenGL ES to get
started
Active community & tests
Lots of tools
It is Simple
Why Cocos2d?
Thursday, August 2, 12
8. sprite: player
Action: CCMoveTo
layer: GameLayer
scene: GameScene
UIView:EAGLView
Thursday, August 2, 12
9. Game has Scenes
Each Scene has some number of Layers
Layers capture user interaction and contain
sprites
Director manages the scenes
Cocos2d Basics
Thursday, August 2, 12
10. Anything thats gets drawn
or contains things that get
drawn is a CCNode
Thursday, August 2, 12
13. They can contain other CCNode nodes
(addChild, getChildByTag, removeChild, etc)
They can schedule periodic callback
(schedule, unschedule, etc)
They can execute actions (runAction,
stopAction, etc)
main features
Thursday, August 2, 12
14. Features of CCNode:
- position
- scale (x, y)
- rotation (in degrees, clockwise)
- CCCamera (an interface to gluLookAt )
- CCGridBase (to do mesh transformations)
- anchor point
- size
- visible
- z-order
- openGL z position
Thursday, August 2, 12
17. CCLayer is a subclass of CCNode that
implements:
- UIAccelerometerDelegate
- CCStandardTouchDelegate
- CCTargetedTouchDelegate
Thursday, August 2, 12
19. (NSSet*)touches
NO
targetedHandlersCount>o
standardHandlersCount>o
YES
foreach...in touches YES
foreach...in
mutablesTouches
foreach...in handlers
ccTouchesBegan/Move/
End
NO
ccTouchBegan
YES
ccTouchMove/End
NO
End
swallowsTouches NO
YES
remove from
mutableTouches
Thursday, August 2, 12
21. You must register as a targeted touch delegate
ccTouchBegan be invoked separately for each of the
available touches
The ccTouchBegan method return YES to indicate
a touch you care about
The swallowsTouches property return YES to
indicate only this layer handle the touch
Targeted Touch Delegate
Thursday, August 2, 12
22. A sample:
How dose CCMenu work?
Thursday, August 2, 12
26. Priority = 2
GameLayer
Priority = 1
Priority = 0
Which Layer handle touch events?
Thursday, August 2, 12
27. Return YES to claim the touch.
-(BOOL)ccTouchBegan
! //* priority used by the menu for the event handler
! kCCMenuHandlerPriority = -128,
Thursday, August 2, 12
42. Box2D is C++
2D rigid physics simulation engine with continuous collision
detection
All you files thats touch C++ or include it must be Objective-
C++(.mm)
Tuned for 1 meter sized objects
Box2D v2.2.0 User Manual
COCOS2D+BOX2D
Thursday, August 2, 12
45. what have the template done for us?
initPhysics
createMenu
draw
addNewSpriteAtPosition
update
ccTouchesEnded
Thursday, August 2, 12
46. ! b2Vec2 gravity;
! gravity.Set(0.0f, -10.0f);
! world = new b2World(gravity);
!
! // Do we want to let bodies sleep?
! world->SetAllowSleeping(true);
!
! world->SetContinuousPhysics(true);
!
! m_debugDraw = new GLESDebugDraw( PTM_RATIO );
! world->SetDebugDraw(m_debugDraw);
!
! uint32 flags = 0;
! flags += b2Draw::e_shapeBit;
! m_debugDraw->SetFlags(flags);! !
!
! // Define the ground body.
! b2BodyDef groundBodyDef;
! groundBodyDef.position.Set(0, 0); // bottom-left corner
!
! // Call the body factory which allocates memory for the ground body
! // from a pool and creates the ground box shape (also from a pool).
! // The body is also added to the world.
! b2Body* groundBody = world->CreateBody(&groundBodyDef);
!
! // Define the ground box shape.
! b2EdgeShape groundBox;! !
!
! // bottom
! groundBox.Set(b2Vec2(0,0), b2Vec2(s.width/PTM_RATIO,0));
! groundBody->CreateFixture(&groundBox,0);
Thursday, August 2, 12
48. sprite = [CCSprite spriteWithFile:spriteFileName];
[sprite setPosition:ccp(p.x,p.y)]; // Remember ccp() is macro for
CGPointMake()
[self addChild:sprite];
}
! }
!
! // Define the dynamic body.
! b2BodyDef bodyDef;
! bodyDef.type = b2_dynamicBody;
! bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
! bodyDef.userData = sprite;
!
! // Tell the physics world to create the body
! b2Body *body = world->CreateBody(&bodyDef);
!
! // Define another box shape for our dynamic body.
! b2PolygonShape dynamicBox;
! dynamicBox.SetAsBox(boxDimensions.x, boxDimensions.y);
! // Define the dynamic body fixture.
! b2FixtureDef fixtureDef;
! fixtureDef.shape = &dynamicBox;!
! fixtureDef.density = boxDensity;
! fixtureDef.friction = 0.3f;
! fixtureDef.restitution = 0.5f; // 0 is a lead ball, 1 is a super bouncy ball
! body->CreateFixture(&fixtureDef);
Thursday, August 2, 12
49. -(void) update: (ccTime) dt
{
! //It is recommended that a fixed time step is used with Box2D for stability
! //of the simulation, however, we are using a variable time step here.
! //You need to make an informed choice, the following URL is useful
! //http://gafferongames.com/game-physics/fix-your-timestep/
!
! int32 velocityIterations = 8;
! int32 positionIterations = 1; Simulating the world(of Box2D)
!
! // Instruct the world to perform a single step of simulation. It is
! // generally best to keep the time step and iterations fixed.
! world->Step(1.0f/60.0f, velocityIterations, positionIterations);
! //Iterate over the bodies in the physics world
! for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
! {
! ! if (b->GetUserData() != NULL) {
! ! ! //Synchronize the AtlasSprites position and rotation with the
corresponding body
! ! ! CCSprite *myActor = (CCSprite*)b->GetUserData();
! ! ! myActor.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b-
>GetPosition().y * PTM_RATIO);
! ! ! myActor.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
! ! }!
! }!
}
Thursday, August 2, 12
55. music & effect
[[SimpleAudioEngine sharedEngine]
playBackgroundMusic:@"Penguino.mp3"];
[[SimpleAudioEngine sharedEngine]
playEffect:@"PenguinSqueak3.mp3"];
setp 6
Thursday, August 2, 12
56. Labels and Fonts
Particle system
Tiled maps
Draw & update
Effects & Grid & Camera
Transitions
DrawingPrimitives
RenderTexture
Shader (2.0)
......
more features
Thursday, August 2, 12