Skip to content

Commit

Permalink
use of auto
Browse files Browse the repository at this point in the history
  • Loading branch information
slackmoehrle committed Aug 28, 2014
1 parent 1d5ca59 commit 8088dd8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 59 deletions.
35 changes: 17 additions & 18 deletions 4.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@

## What are Sprites
A Sprite is a 2D graphic.

## Creating Sprites
### Creating a Textured Sprite
```cpp
cococ2d::Sprite* mySprite = cocos2d::Sprite::create("mysprite.png");
auto mySprite = cocos2d::Sprite::create("mysprite.png");
```
### Creating an Untextured Sprite
```cpp
cocos2d::Sprite* mySprite = cocos2d::Sprite::create();
auto mySprite = cocos2d::Sprite::create();
```
### Creating a Sprite From a SpriteFrame
```cpp
cocos2d::SpriteFrame* frame = cocos2d::SpriteFrame::createWithTexture(texture, rect, offset);
auto frame = cocos2d::SpriteFrame::createWithTexture(texture, rect, offset);

cocos2d::Sprite* mysprite = cocos2d::Sprite::createWithSpriteFrame(frame);
auto mysprite = cocos2d::Sprite::createWithSpriteFrame(frame);
```
### Creating a Sprite From SpriteCache
```cpp
cocos2d::Sprite* mysprite = cocos2d::Sprite::createWithSpriteFrameName("mysprite.png");
auto mysprite = cocos2d::Sprite::createWithSpriteFrameName("mysprite.png");
```
### Creating a Sprite From a Rect
```cpp
cococ2d::Sprite* mySprite = cocos2d::Sprite::create("mysprite.png", cocos2d::Rect(0,0,40,40));
auto mySprite = cocos2d::Sprite::create("mysprite.png", cocos2d::Rect(0,0,40,40));
```
## Sprite Manipulation
After creating a Sprite, there are a variety of functions that can be applied to it.

Given:
```cpp
cococ2d::Sprite* mySprite = cocos2d::Sprite::create("mysprite.png");
auto mySprite = cocos2d::Sprite::create("mysprite.png");
```
### Resizing
```cpp
Expand Down Expand Up @@ -64,18 +64,18 @@ A SpriteSheet a way to combine sprites into a single texture while reducing the
Load your SpriteSheet into the `SpriteFrameCache`, probably in AppDelegate:
```cpp
cocos2d::SpriteFrameCache* _spritecache = cocos2d::SpriteFrameCache::getInstance();
auto _spritecache = cocos2d::SpriteFrameCache::getInstance();
_spritecache->addSpriteFramesWithFile("sprites.plist");
```
A few examples of working with a SpriteSheet:

Creating a `Sprite` from the `SpriteFrameCache`
```cpp
cocos2d::Sprite* mySprite = cocos2d::Sprite::createWithSpriteFrameName("mysprite.png");
auto mySprite = cocos2d::Sprite::createWithSpriteFrameName("mysprite.png");
```
Creating an `Animation` from `SpriteFrameCache`
```cpp
cocos2d::Animation* anim1 = cocos2d::Animation::create();
auto anim1 = cocos2d::Animation::create();
anim1->addSpriteFrameWithFile("enemystand.png");
anim1->addSpriteFrameWithFile("enemymove1.png");
anim1->addSpriteFrameWithFile("enemymove2.png");
Expand All @@ -84,7 +84,7 @@ anim1->addSpriteFrameWithFile("enemymove4.png");
anim1->addSpriteFrameWithFile("enemymove5.png");
anim1->addSpriteFrameWithFile("enemyjump.png");

cocos2d::Animate* animation = cocos2d::Animate::create(anim1);
auto animation = cocos2d::Animate::create(anim1);
animation->setDuration(0.2f);
sprite->runAction(cocos2d::RepeatForever::create(animation));
```
Expand All @@ -95,17 +95,16 @@ A `Node` can be thought of as a root object. It doesn't draw anything, but can b
## Creating Nodes
A simple `Node`:
```cpp
cocos2d::Node* myNode = cocos2d::Node::create();
auto myNode = cocos2d::Node::create();
```

Creating a `Node` with children:
```cpp
cocos2d::Node* myNode = cocos2d::Node::create();
auto myNode = cocos2d::Node::create();

cococ2d::Sprite* mySprite1 = cocos2d::Sprite::create("mysprite1.png");
auto mySprite1 = cocos2d::Sprite::create("mysprite1.png");
myNode->addChild(mySprite1);

cococ2d::Sprite* mySprite2 = cocos2d::Sprite::create("mysprite2.png");
auto mySprite2 = cocos2d::Sprite::create("mysprite2.png");
myNode->addChild(mySprite2);
```
```
38 changes: 19 additions & 19 deletions 5.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Fades In a Node that implements the RGBA protocol.
It modifies the opacity from 0 to 255. The "reverse" of this action is `FadeOut`
```cpp
// appy a fade to 200
cocos2d::FadeIn* fadeIn = cocos2d::FadeIn::create(200.0);
auto fadeIn = cocos2d::FadeIn::create(200.0);
myNode->runAction(fadeIn);

// fade to 120 over 2 seconds
cocos2d::FadeTo* fadeTo = cocos2d::FadeTo::create(2.0f, 120.0f);
auto fadeTo = cocos2d::FadeTo::create(2.0f, 120.0f);
myNode->runAction(fadeTo);

// then reverse it
Expand All @@ -29,52 +29,52 @@ myNode->runAction(fadeTo->reverse());
Move the Node position over a set period of time
```cpp
// Move a sprite 50 pixels to the right, and 10 pixels to the top over 2 seconds.
cocos2d::MoveBy* moveBy = cocos2d::MoveBy::create(2, cocos2d::Point(50,10));
auto moveBy = cocos2d::MoveBy::create(2, cocos2d::Point(50,10));
myNode->runAction(moveBy);
// Move a sprite to a specic location over 2 seconds.
cocos2d::MoveTo* moveTo = cocos2d::MoveTo::create(2, cocos2d::Point(50,10));
auto moveTo = cocos2d::MoveTo::create(2, cocos2d::Point(50,10));
myNode->runAction(moveTo);
```
### Rotate
Rotates a Node
```cpp
// Rotates a node to a specified change
cocos2d::RotateTo* rotateTo = cocos2d::RotateTo::create(2.0f, 10);
auto rotateTo = cocos2d::RotateTo::create(2.0f, 10);
myNode->runAction(rotateTo);

// Rotates a Node by a specfied change
cocos2d::RotateBy* rotateBy = cocos2d::RotateBy::create(2.0f, 10);
auto rotateBy = cocos2d::RotateBy::create(2.0f, 10);
myNode->runAction(rotateBy);
```
### Scale
Scales a Node
```cpp
// Scale uniformly by 10 over 2 seconds
cocos2d::ScaleBy* scaleBy = cocos2d::ScaleBy::create(2.0f, 10.0f);
auto scaleBy = cocos2d::ScaleBy::create(2.0f, 10.0f);
myNode->runAction(scaleBy);
// Scale X by 10 and Y by 20 over 2 seconds
cocos2d::ScaleBy* scaleBy = cocos2d::ScaleBy::create(2.0f, 10.0f, 20.0f);
auto scaleBy = cocos2d::ScaleBy::create(2.0f, 10.0f, 20.0f);
myNode->runAction(scaleBy);
// Scale to uniformly to 10 over 2 seconds
cocos2d::ScaleTo* scaleTo = cocos2d::ScaleTo::create(2.0f, 10.0f);
auto scaleTo = cocos2d::ScaleTo::create(2.0f, 10.0f);
myNode->runAction(scaleTo);
// Scale X to 10 and Y to 20 over 2 seconds
cocos2d::ScaleTo* scaleTo = cocos2d::ScaleTo::create(2.0f, 10.0f, 20.0f);
auto scaleTo = cocos2d::ScaleTo::create(2.0f, 10.0f, 20.0f);
myNode->runAction(scaleTo);
```
### Tint
Tints a Node that implements the NodeRGB protocol from current tint to a custom one.
```cpp
// Tints a node to the specified RGB values
cocos2d::TintTo* tintTo = cocos2d::TintTo::create(2.0f, 120.0f, 132.0f, 196.0f);
auto tintTo = cocos2d::TintTo::create(2.0f, 120.0f, 132.0f, 196.0f);
myNode->runAction(tintTo);

// Tints a node BY the delta of the specified RGB values.
cocos2d::TintBy* tintBy = cocos2d::TintBy::create(2.0f, 10.0f, 2.0f, 20.0f);
auto tintBy = cocos2d::TintBy::create(2.0f, 10.0f, 2.0f, 20.0f);
myNode->runAction(tintBy);
```
### Tweening and Easing
Expand All @@ -85,9 +85,9 @@ Sequences are a series of events to be executed sequentially.
### An example sequence
```cpp
// create a few actions.
cocos2d::JumpBy* jump = cocos2d::JumpBy::create(0.5, cocos2d::Point(0, 0), 100, 1);
auto jump = cocos2d::JumpBy::create(0.5, cocos2d::Point(0, 0), 100, 1);
cocos2d::RotateTo* rotate = cocos2d::RotateTo::create(2.0f, 10);
auto rotate = cocos2d::RotateTo::create(2.0f, 10);
// create a few callbacks
auto callbackJump = cocos2d::CallFunc::create([](){
Expand All @@ -99,7 +99,7 @@ auto callbackRotate = cocos2d::CallFunc::create([](){
});
// create a sequence with the actions and callbacks
cocos2d::Sequence* seq = cocos2d::Sequence::create(jump, callbackJump, rotate, callbackRotate, NULL);
auto seq = cocos2d::Sequence::create(jump, callbackJump, rotate, callbackRotate, NULL);
// run it
myNode->runAction(seq);
Expand All @@ -114,13 +114,13 @@ It executed the following sequentially:
Spawn is very similar to `Sequence` except that all actions will run at the same time.
```cpp
// create 2 actions and run a Spawn on a Sprite
cocos2d::Sprite* mySprite = cocos2d::Sprite::create("circle.png");
auto mySprite = cocos2d::Sprite::create("circle.png");

cocos2d::MoveBy* actionBy = cocos2d::MoveBy::create(10, Point(400,100));
auto actionBy = cocos2d::MoveBy::create(10, Point(400,100));

cocos2d::FadeTo* fadeTo = cocos2d::FadeTo::create(2.0f, 120.0f);
auto fadeTo = cocos2d::FadeTo::create(2.0f, 120.0f);

cocos2d::Spawn * mySpawn = cocos2d::Spawn::createWithTwoActions( actionBy, fadeTo );
auto mySpawn = cocos2d::Spawn::createWithTwoActions( actionBy, fadeTo );

mySprite->runAction(mySpawn);
```
Expand Down
26 changes: 14 additions & 12 deletions 6.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ A `Node` tree controls the draw order of GUI elements. This might also be referr
A simple `Node` tree laid out in code:
```cpp
// create a simple scene
cocos2d::Scene* myScene = cocos2d::Scene::create();
auto myScene = cocos2d::Scene::create();

// create a simple layer
cocos2d::Layer* myLayer = cocos2d::Layer::create();
auto myLayer = cocos2d::Layer::create();

// a few Sprites
cocos2d::Sprite* sprite1 = cocos2d::Sprite::create("sprite.png");
cocos2d::Sprite* sprite2 = cocos2d::Sprite::create("sprite.png");
cocos2d::Sprite* sprite3 = cocos2d::Sprite::create("sprite.png");
auto sprite1 = cocos2d::Sprite::create("sprite.png");
auto sprite2 = cocos2d::Sprite::create("sprite.png");
auto sprite3 = cocos2d::Sprite::create("sprite.png");

// a few more Sprites
cocos2d::Sprite* spriteA = cocos2d::Sprite::create("sprite2.png");
cocos2d::Sprite* spriteB = cocos2d::Sprite::create("sprite2.png");
cocos2d::Sprite* spriteC = cocos2d::Sprite::create("sprite2.png");
auto spriteA = cocos2d::Sprite::create("sprite2.png");
auto spriteB = cocos2d::Sprite::create("sprite2.png");
auto spriteC = cocos2d::Sprite::create("sprite2.png");

sprite1->addChild(spriteA); // spriteA is now a child of sprite1
sprite2->addChild(spriteB); // spriteB is now a child of sprite2
Expand All @@ -57,25 +57,27 @@ You might need to move between `Scenes` in your game. Perhaps starting a new gam
### Ways to transition between Scenes
```cpp
cocos2d::Scene* myScene = cocos2d::Scene::create();
auto myScene = cocos2d::Scene::create();
// runWithScene - use this for the first Scene only
cocos2d::Director::getInstance()->runWithScene(myScene);
// replaceScene - replace a scene outright
cocos2d::Director::getInstance()->replaceScene(myScene);
// pushScene - suspends the execution of the running scene, pushing it on the stack of suspended scenes. Only call this if there is a running scene
// pushScene - suspends the execution of the running scene, pushing it on the stack
// of suspended scenes. Only call this if there is a running scene
cocos2d::Director::getInstance()->pushScene(myScene);
// popScene - This scene will replace the running one. The running scene will be deleted. Only call this if there is a running scene
// popScene - This scene will replace the running one. The running scene will be
// deleted. Only call this if there is a running scene
cocos2d::Director::getInstance()->popScene(myScene);
```

### Transition Scenes with effects
You can add visual effects to your `Scene` transitions
```cpp
cocos2d::Scene* myScene = cocos2d::Scene::create();
auto myScene = cocos2d::Scene::create();

// Transition Fade
cocos2d::Director::getInstance()->replaceScene(cocos2d::TransitionFade::create(0.5, myScene, cocos2d::Color3B(0,255,255)));
Expand Down
20 changes: 10 additions & 10 deletions 7.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ There are a few different types of Labels you can choose from.
#### BMFont
`BMFont` is a label type that uses a bitmap font.
```cpp
cocos2d::Label* myLabel = cocos2d::Label::createWithBMFont("fonts/Font.fnt", "My Label Text");
auto myLabel = cocos2d::Label::createWithBMFont("fonts/Font.fnt", "My Label Text");
```
#### TTF
`TTF` is a label type that uses a True Type Font.
```cpp
cocos2d::Label* myLabel = cocos2d::Label::createWithTTF("Arial", "My Label Text", 16);
auto myLabel = cocos2d::Label::createWithTTF("Arial", "My Label Text", 16);
```
#### Label Atlas

#### SystemFont
`SystemFont` is a label type that usese the default system font and font size.
```cpp
cocos2d::Label* myLabel3 = cocos2d::Label::createWithSystemFont("My Label Text", "Arial", 16);
auto myLabel3 = cocos2d::Label::createWithSystemFont("My Label Text", "Arial", 16);
```

### Menu&Menu Items
Expand All @@ -48,28 +48,28 @@ A `Menu` is a way to navigate through game options. Menus often contain options
A `Menu` is made up of a base node called `Menu`. It is easy to create a `Menu`.

```cpp
cocos2d::Menu* myMenu = cocos2d::Menu::create();
auto myMenu = cocos2d::Menu::create();
```
#### Menu Items and adding to a Menu
`MenuItems` are the meat of any `Menu`. Menu Items usually have a `normal` and a `selected` state as well as a callback...something that happens when the `MenuItem` is selected.

```cpp
// creating a menu with a single item
cocos2d::MenuItemImage* closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
auto closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

cocos2d::Mneu* menu = cocos2d::Menu::create(closeItem, NULL);
this->addChild(menu, 1);

// creating a Menu from a Vector of items
cocos2d::Vector<cocos2d::MenuItem*> MenuItems;

cocos2d::MenuItemImage* closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
auto closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

MenuItems.pushBack(closeItem);

/* repeat for as many menu items as needed */

cocos2d::Menu* menu = cocos2d::Menu::createWithArray(MenuItems);
auto menu = cocos2d::Menu::createWithArray(MenuItems);
this->addChild(menu, 1);
```
#### Lambdas
Expand All @@ -93,7 +93,7 @@ std::function<void()> myFunction = []()
auto action2 = cocos2d::CallFunc::create(myFunction);
// creating a MenuItemImage with a lambda callback
cocos2d::MenuItemImage* closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", [&](Object *sender){
auto closeItem = cocos2d::MenuItemImage::create("CloseNormal.png", "CloseSelected.png", [&](cocos2d::Ref* sender){
// your code here
});
```
Expand Down Expand Up @@ -121,7 +121,7 @@ cocos2d::MenuItemImage* closeItem = cocos2d::MenuItemImage::create("CloseNormal.
### ListView
### PageView
### Layout
#### HBOX & VBox
#### Relative Layout
### HBOX & VBox
### Relative Layout


0 comments on commit 8088dd8

Please sign in to comment.