#include “HelloWorldScene.h”
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// ‘scene’ is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// ‘layer’ is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene~~>addChild;
} while ;
// return the scene
return scene;
}
// on “init” you need to initialize your instance
bool HelloWorld::init
{
setTouchEnabled;
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF);
//////////////////////////////////////////////////////////////////////////
// add your codes below…
//////////////////////////////////////////////////////////////////////////
// 1. Add a menu item with “X” image, which is clicked to quit the program.
// Create a “close” menu item with close icon, it’s an auto release object.
CCMenuItemImage **pCloseItem = CCMenuItemImage::create);
CC_BREAK_IF;
// Place the menu item bottom-right conner.
pCloseItem~~>setPosition~~>getWinSize.width~~ 20, 20));
// Create a menu with the “close” menu item, it’s an auto release object.
CCMenu* pMenu = CCMenu::create;
pMenu~~>setPosition;
CC_BREAK_IF;
// Add the menu to HelloWorld layer as a child layer.
this~~>addChild;
// 2. Add a label shows “Hello World”.
// Create a label and initialize with string “Hello World”.
CCLabelTTF* pLabel = CCLabelTTF::create;
CC_BREAK_IF;
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector~~>getWinSize;
pLabel~~>setPosition);
// Add the label to HelloWorld layer as a child layer.
this~~>addChild;
// 3. Add add a splash screen, show the cocos2d splash image.
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
CCSprite *player = CCSprite::create );
player~~>setPosition.width/2, winSize.height/2) );
this~~>addChild;
bRet = true;
} while ;
this~~>schedule, 1.0 );
return bRet;
}
void HelloWorld::menuCloseCallback
{
// “close” menu item clicked
CCDirector::sharedDirector~~>end;
}
void HelloWorld::addTarget
{
CCSprite **target = CCSprite::create );
// Determine where to spawn the target along the Y axis
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
int minY = target~~>getContentSize.height/2;
int maxY = winSize.height
-
target~~>getContentSize.height/2;
int rangeY = maxY~~ minY;
// srand );
int actualY = rangeY ) + minY;// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated
target->setPosition(
ccp(winSize.width + (target->getContentSize().width/2),
actualY) );
this->addChild(target);// Determine speed of the target
int minDuration = (int)2.0;
int maxDuration = (int)4.0;
int rangeDuration = maxDuration - minDuration;
// srand( TimGetTicks() );
int actualDuration = ( rand() rangeDuration )
- minDuration;
// Create the actions
CCFiniteTimeAction** actionMove =
CCMoveTo::createactualDuration,
ccp.width/2, actualY) );
CCFiniteTimeAction* actionMoveDone =
CCCallFuncN::create);
target~~>runAction );
}
void HelloWorld::spriteMoveFinished
{
CCSprite**sprite = sender;
this~~>removeChild(sprite, true);
}
void HelloWorld::gameLogic(float ct)
{
this~~>addTarget;
}
void HelloWorld::ccTouchesEnded
{
// Choose one of the touches to work with
CCTouch* touch = );
CCPoint location = touch~~>locationInView();
location = CCDirector::sharedDirector()>convertToGL;>getWinSize;
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector
CCSprite *projectile = CCSprite::create);
projectile~~>setPosition );
// Determinie offset of location to projectile
int offX = location.x~~ projectile~~>getPosition.x;
int offY = location.y~~ projectile~~>getPosition.y;
// Bail out if we are shooting down or backwards
if return;
// Ok to add now~~ we’ve double checked position
this~~>addChild;
// Determine where we wish to shoot the projectile to
int realX = winSize.width
- .width/2);
float ratio = offY / offX;
int realY = + projectile~~>getPosition().y;
CCPoint realDest = ccp(realX, realY);
// Determine the length of how far we’re shooting
int offRealX = realX - projectile~~>getPosition.x;
int offRealY = realY~~ projectile~~>getPosition.y;
float length = sqrtf
- );
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
projectile~~>runAction( CCSequence::actions(
CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this,
callfuncN_selector(HelloWorld::spriteMoveFinished)),
NULL) );
}