mirror of https://github.com/axmolengine/axmol.git
issue #157 Cocos2dSimpleGame, ninjia is coming!
This commit is contained in:
parent
1b385bfe20
commit
61ccb9f59e
|
@ -6,11 +6,33 @@
|
||||||
|
|
||||||
#include "cocos2d.h"
|
#include "cocos2d.h"
|
||||||
|
|
||||||
#include "AppDelegate.h"
|
#include "Cocos2dSimpleGameAppDelegate.h"
|
||||||
|
#include "cocos2d.h"
|
||||||
#include "HelloWorldScene.h"
|
#include "HelloWorldScene.h"
|
||||||
|
|
||||||
using namespace cocos2d;
|
using namespace cocos2d;
|
||||||
|
|
||||||
|
// the works are the same as NSObject<UIApplicationDelegate>::applicationDidFinishLaunching of cocos2d-iphone
|
||||||
|
bool AppDelegate::initCocos2d()
|
||||||
|
{
|
||||||
|
// init director
|
||||||
|
CCDirector *pDirector = CCDirector::getSharedDirector();
|
||||||
|
pDirector->setOpenGLView(m_pMainWnd);
|
||||||
|
|
||||||
|
// sets landscape mode
|
||||||
|
pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
|
||||||
|
|
||||||
|
// turn on display FPS
|
||||||
|
pDirector->setDisplayFPS(true);
|
||||||
|
|
||||||
|
// create a scene. it's an autorelease object
|
||||||
|
CCScene *pScene = HelloWorld::scene();
|
||||||
|
|
||||||
|
// run
|
||||||
|
CCDirector::getSharedDirector()->runWithScene(pScene);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
AppDelegate::AppDelegate()
|
AppDelegate::AppDelegate()
|
||||||
:m_rcWnd(0, 0, GetScreenWidth(), GetScreenHeight())
|
:m_rcWnd(0, 0, GetScreenWidth(), GetScreenHeight())
|
||||||
|
@ -57,25 +79,3 @@ Boolean AppDelegate::EventHandler(EventType* pEvent)
|
||||||
|
|
||||||
return (bHandled) ? TRUE : CCXApplication::EventHandler(pEvent);
|
return (bHandled) ? TRUE : CCXApplication::EventHandler(pEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the works are the same as NSObject<UIApplicationDelegate>::applicationDidFinishLaunching of cocos2d-iphone
|
|
||||||
bool AppDelegate::initCocos2d()
|
|
||||||
{
|
|
||||||
// init director
|
|
||||||
CCDirector *pDirector = CCDirector::getSharedDirector();
|
|
||||||
pDirector->setOpenGLView(m_pMainWnd);
|
|
||||||
|
|
||||||
// sets landscape mode
|
|
||||||
pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
|
|
||||||
|
|
||||||
// turn on display FPS
|
|
||||||
pDirector->setDisplayFPS(true);
|
|
||||||
|
|
||||||
// create a scene. it's an autorelease object
|
|
||||||
CCScene *pScene = HelloWorld::scene();
|
|
||||||
|
|
||||||
// run
|
|
||||||
CCDirector::getSharedDirector()->runWithScene(pScene);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
|
|
||||||
#include "FirstLayer.h"
|
|
||||||
using namespace cocos2d;
|
|
||||||
|
|
||||||
#define PATH_CLOSE_NORMAL "/NEWPLUS/TDA_DATA/UserData/CloseNormal.png"
|
|
||||||
#define PATH_CLOSE_SELECTED "/NEWPLUS/TDA_DATA/UserData/CloseSelected.png"
|
|
||||||
|
|
||||||
|
|
||||||
// on "init" you need to initialize your instance
|
|
||||||
bool FirstLayer::init()
|
|
||||||
{
|
|
||||||
//////////////////////////////
|
|
||||||
// 1. super init first
|
|
||||||
if ( !CCLayer::init() )
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////
|
|
||||||
// 2. add a label shows "Hello World"
|
|
||||||
|
|
||||||
// create and initialize a label
|
|
||||||
CCLabel* pLabel = CCLabel::labelWithString("HelloWorld", "Thonburi", 64);
|
|
||||||
|
|
||||||
// ask director the window size
|
|
||||||
CGSize size = CCDirector::getSharedDirector()->getWinSize();
|
|
||||||
|
|
||||||
// position the label on the center of the screen
|
|
||||||
pLabel->setPosition( ccp(size.width / 2, size.height / 2) );
|
|
||||||
|
|
||||||
// add the label as a child to this layer
|
|
||||||
this->addChild(pLabel);
|
|
||||||
|
|
||||||
/////////////////////////////
|
|
||||||
// 3. add a menu item with "X" image, which is clicked to quit the program
|
|
||||||
|
|
||||||
// add a "close" icon to exit the progress. it's an autorelease object
|
|
||||||
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
|
|
||||||
PATH_CLOSE_NORMAL,
|
|
||||||
PATH_CLOSE_SELECTED,
|
|
||||||
this,
|
|
||||||
menu_selector(FirstLayer::menuCloseCallback) );
|
|
||||||
pCloseItem->setPosition( ccp(size.width - 20, 20) );
|
|
||||||
|
|
||||||
// create menu, it's an autorelease object
|
|
||||||
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
|
|
||||||
pMenu->setPosition( CGPointZero );
|
|
||||||
this->addChild(pMenu);
|
|
||||||
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FirstLayer::menuCloseCallback(NSObject* pSender)
|
|
||||||
{
|
|
||||||
CCDirector::getSharedDirector()->end();
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
|
|
||||||
#ifndef _HELLOWORLD_LAYER_H_
|
|
||||||
#define _HELLOWORLD_LAYER_H_
|
|
||||||
|
|
||||||
#include "cocos2d.h"
|
|
||||||
|
|
||||||
class FirstLayer : public cocos2d::CCLayer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
|
|
||||||
virtual bool init();
|
|
||||||
|
|
||||||
// a selector callback
|
|
||||||
virtual void menuCloseCallback(NSObject* pSender);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _HELLOWORLD_SCENE_H_
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
//
|
||||||
|
// GameOverScene.m
|
||||||
|
// Cocos2DSimpleGame
|
||||||
|
//
|
||||||
|
// Created by Ray Wenderlich on 2/10/10.
|
||||||
|
// Copyright 2010 Ray Wenderlich. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "GameOverScene.h"
|
||||||
|
#include "HelloWorldScene.h"
|
||||||
|
|
||||||
|
using namespace cocos2d;
|
||||||
|
|
||||||
|
bool GameOverScene::init()
|
||||||
|
{
|
||||||
|
if( CCScene::init() )
|
||||||
|
{
|
||||||
|
this->_layer = GameOverLayer::node();
|
||||||
|
this->addChild(_layer);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GameOverScene::~GameOverScene()
|
||||||
|
{
|
||||||
|
_layer->release();
|
||||||
|
_layer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////
|
||||||
|
bool GameOverLayer::init()
|
||||||
|
{
|
||||||
|
if ( CCColorLayer::initWithColor( ccc4(255,255,255,255) ) )
|
||||||
|
{
|
||||||
|
CGSize winSize = CCDirector::getSharedDirector()->getWinSize();
|
||||||
|
this->_label = CCLabel::labelWithString("", "Artial", 32);
|
||||||
|
_label->setColor( ccc3(0, 0, 0) );
|
||||||
|
_label->setPosition( ccp(winSize.width/2, winSize.height/2) );
|
||||||
|
this->addChild(_label);
|
||||||
|
|
||||||
|
this->runAction( CCSequence::actions(
|
||||||
|
CCDelayTime::actionWithDuration(3),
|
||||||
|
CCCallFunc::actionWithTarget(this, callfunc_selector(GameOverLayer::gameOverDone)),
|
||||||
|
NULL));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameOverLayer::gameOverDone()
|
||||||
|
{
|
||||||
|
CCDirector::getSharedDirector()->replaceScene( HelloWorld::scene() );
|
||||||
|
}
|
||||||
|
|
||||||
|
GameOverLayer::~GameOverLayer()
|
||||||
|
{
|
||||||
|
_label->release();
|
||||||
|
_label = NULL;
|
||||||
|
}
|
|
@ -0,0 +1,91 @@
|
||||||
|
//
|
||||||
|
// GameOverScene.h
|
||||||
|
// Cocos2DSimpleGame
|
||||||
|
//
|
||||||
|
// Created by Ray Wenderlich on 2/10/10.
|
||||||
|
// Copyright 2010 Ray Wenderlich. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef _GAME_OVER_SCENE_H_
|
||||||
|
#define _GAME_OVER_SCENE_H_
|
||||||
|
|
||||||
|
#include "cocos2d.h"
|
||||||
|
|
||||||
|
class GameOverLayer : public cocos2d::CCColorLayer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~GameOverLayer();
|
||||||
|
|
||||||
|
// LAYER_NODE_FUNC(GameOverLayer);
|
||||||
|
static GameOverLayer* node()
|
||||||
|
{
|
||||||
|
GameOverLayer *pRet = new GameOverLayer();
|
||||||
|
if (pRet && pRet->init())
|
||||||
|
{
|
||||||
|
pRet->autorelease();
|
||||||
|
return pRet;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CCX_SAFE_DELETE(pRet)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool init();
|
||||||
|
void gameOverDone();
|
||||||
|
|
||||||
|
inline cocos2d::CCLabel* getLabel() { return _label; };
|
||||||
|
protected:
|
||||||
|
cocos2d::CCLabel *_label;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class GameOverScene : public cocos2d::CCScene
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~GameOverScene();
|
||||||
|
bool init();
|
||||||
|
// SCENE_NODE_FUNC(GameOverScene);
|
||||||
|
static GameOverScene* node()
|
||||||
|
{
|
||||||
|
GameOverScene *pRet = new GameOverScene();
|
||||||
|
if (pRet && pRet->init())
|
||||||
|
{
|
||||||
|
pRet->autorelease();
|
||||||
|
return pRet;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CCX_SAFE_DELETE(pRet)
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline GameOverLayer* getLayer() {return _layer; };
|
||||||
|
protected:
|
||||||
|
GameOverLayer *_layer;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _GAME_OVER_SCENE_H_
|
||||||
|
|
||||||
|
/*
|
||||||
|
#import "cocos2d.h"
|
||||||
|
|
||||||
|
@interface GameOverLayer : CCColorLayer {
|
||||||
|
CCLabel *_label;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property (nonatomic, retain) CCLabel *label;
|
||||||
|
|
||||||
|
@end
|
||||||
|
|
||||||
|
@interface GameOverScene : CCScene {
|
||||||
|
GameOverLayer *_layer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@property (nonatomic, retain) GameOverLayer *layer;
|
||||||
|
|
||||||
|
@end
|
||||||
|
*/
|
|
@ -1,25 +0,0 @@
|
||||||
|
|
||||||
#include "HelloWorld.h"
|
|
||||||
#include "FirstLayer.h"
|
|
||||||
using namespace cocos2d;
|
|
||||||
|
|
||||||
CCScene* HelloWorld::scene()
|
|
||||||
{
|
|
||||||
bool bRet = false;
|
|
||||||
|
|
||||||
// 'pScene' is an autorelease object
|
|
||||||
CCScene *pScene = CCScene::node();
|
|
||||||
|
|
||||||
// 'pLayer' is an autorelease object
|
|
||||||
FirstLayer *pLayer = new FirstLayer;
|
|
||||||
pLayer->init();
|
|
||||||
|
|
||||||
// add layer as a child to scene
|
|
||||||
pScene->addChild(pLayer);
|
|
||||||
|
|
||||||
// release layer, it's "new" by us.
|
|
||||||
pLayer->release();
|
|
||||||
|
|
||||||
// return the scene
|
|
||||||
return pScene;
|
|
||||||
}
|
|
|
@ -1,14 +0,0 @@
|
||||||
|
|
||||||
#ifndef _HELLOWORLD_SCENE_H_
|
|
||||||
#define _HELLOWORLD_SCENE_H_
|
|
||||||
|
|
||||||
#include "cocos2d.h"
|
|
||||||
|
|
||||||
class HelloWorld // Here's not a child of CCScene!
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
|
|
||||||
static cocos2d::CCScene* scene();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // _HELLOWORLD_SCENE_H_
|
|
|
@ -0,0 +1,292 @@
|
||||||
|
|
||||||
|
#include "HelloWorldScene.h"
|
||||||
|
// #include "SimpleAudioEngine.h"
|
||||||
|
#include "GameOverScene.h"
|
||||||
|
#include <math.h>
|
||||||
|
using namespace cocos2d;
|
||||||
|
|
||||||
|
#define PATH_CLOSE_NORMAL "/NEWPLUS/TDA_DATA/UserData/CloseNormal.png"
|
||||||
|
#define PATH_CLOSE_SELECTED "/NEWPLUS/TDA_DATA/UserData/CloseSelected.png"
|
||||||
|
#define IMG_PLAYER "/NEWPLUS/TDA_DATA/UserData/player.png"
|
||||||
|
#define IMG_PROJECTILE "/NEWPLUS/TDA_DATA/UserData/projectile.png"
|
||||||
|
#define IMG_TARGET "/NEWPLUS/TDA_DATA/UserData/target.png"
|
||||||
|
|
||||||
|
CCScene* HelloWorld::scene()
|
||||||
|
{
|
||||||
|
bool bRet = false;
|
||||||
|
|
||||||
|
// 'pScene' is an autorelease object
|
||||||
|
CCScene *pScene = CCScene::node();
|
||||||
|
|
||||||
|
// 'pLayer' is an autorelease object
|
||||||
|
HelloWorld *pLayer = new HelloWorld;
|
||||||
|
pLayer->init();
|
||||||
|
|
||||||
|
// add layer as a child to scene
|
||||||
|
pScene->addChild(pLayer);
|
||||||
|
|
||||||
|
// release layer, it's "new" by us.
|
||||||
|
pLayer->release();
|
||||||
|
|
||||||
|
// return the scene
|
||||||
|
return pScene;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HelloWorld::spriteMoveFinished(CCNode* sender)
|
||||||
|
{
|
||||||
|
TagSprite *sprite = (TagSprite *)sender;
|
||||||
|
this->removeChild(sprite, true);
|
||||||
|
|
||||||
|
if (sprite->getTag() == 1) // target
|
||||||
|
{
|
||||||
|
_targets->removeObject(sprite);
|
||||||
|
|
||||||
|
GameOverScene *gameOverScene = GameOverScene::node();
|
||||||
|
gameOverScene->getLayer()->getLabel()->setString("You Lose :[");
|
||||||
|
CCDirector::getSharedDirector()->replaceScene(gameOverScene);
|
||||||
|
}
|
||||||
|
else if (sprite->getTag() == 2) // projectile
|
||||||
|
{
|
||||||
|
_projectiles->removeObject(sprite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HelloWorld::addTarget()
|
||||||
|
{
|
||||||
|
TagSprite *target = (TagSprite*)CCSprite::spriteWithFile(IMG_TARGET, CGRectMake(0,0,27,40));
|
||||||
|
|
||||||
|
// Determine where to spawn the target along the Y axis
|
||||||
|
CGSize winSize = CCDirector::getSharedDirector()->getWinSize();
|
||||||
|
int minY = (int)( target->getContentSize().height / 2 );
|
||||||
|
int maxY = (int)( winSize.height - target->getContentSize().height / 2 );
|
||||||
|
int rangeY = maxY - minY;
|
||||||
|
|
||||||
|
srand( TimGetTicks() );
|
||||||
|
int actualY = ( rand() % rangeY ) + minY;
|
||||||
|
|
||||||
|
// Create the target slightly off-screen along the right edge,
|
||||||
|
// and along a random position along the Y axis as calculated above
|
||||||
|
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::actionWithDuration( (ccTime)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) );
|
||||||
|
CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished));
|
||||||
|
target->runAction( CCSequence::actions(actionMove, actionMoveDone, NULL) );
|
||||||
|
|
||||||
|
// Add to targets array
|
||||||
|
target->setTag(1);
|
||||||
|
_targets->addObject(target);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void HelloWorld::gameLogic(ccTime dt)
|
||||||
|
{
|
||||||
|
this->addTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// on "init" you need to initialize your instance
|
||||||
|
bool HelloWorld::init()
|
||||||
|
{
|
||||||
|
// always call "super" init
|
||||||
|
if ( !CCColorLayer::initWithColor( ccc4(255,255,255,255) ) )
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// cocos2d-uphone: add a menu item with "X" image, which is clicked to quit the program
|
||||||
|
// add a "close" icon, it's an autorelease object
|
||||||
|
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
|
||||||
|
PATH_CLOSE_NORMAL,
|
||||||
|
PATH_CLOSE_SELECTED,
|
||||||
|
this,
|
||||||
|
menu_selector(HelloWorld::menuCloseCallback) );
|
||||||
|
pCloseItem->setPosition( ccp( CCDirector::getSharedDirector()->getWinSize().width - 20, 20) );
|
||||||
|
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); // create menu, it's an autorelease object
|
||||||
|
pMenu->setPosition( CGPointZero );
|
||||||
|
this->addChild(pMenu);
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////////////
|
||||||
|
// add your code below
|
||||||
|
|
||||||
|
// Enable touch events
|
||||||
|
this->setIsTouchEnabled(true);
|
||||||
|
|
||||||
|
// Initialize arrays
|
||||||
|
_targets = new NSMutableArray<TagSprite*>;
|
||||||
|
_projectiles = new NSMutableArray<TagSprite*>;
|
||||||
|
|
||||||
|
// Get the dimensions of the window for calculation purposes
|
||||||
|
CGSize winSize = CCDirector::getSharedDirector()->getWinSize();
|
||||||
|
|
||||||
|
// Add the player to the middle of the screen along the y-axis,
|
||||||
|
// and as close to the left side edge as we can get
|
||||||
|
// Remember that position is based on the anchor point, and by default the anchor
|
||||||
|
// point is the middle of the object.
|
||||||
|
CCSprite *player = CCSprite::spriteWithFile(IMG_PLAYER, CGRectMake(0, 0, 27, 40) );
|
||||||
|
player->setPosition( ccp(player->getContentSize().width / 2, winSize.height/2) );
|
||||||
|
this->addChild(player);
|
||||||
|
|
||||||
|
// Call game logic about every second
|
||||||
|
this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );
|
||||||
|
this->schedule( schedule_selector(HelloWorld::update) );
|
||||||
|
|
||||||
|
// Start up the background music
|
||||||
|
// SimpleAudioEngine->getSharedEngine()->playBackgroundMusic("background-music-aac.caf");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HelloWorld::update(ccTime dt)
|
||||||
|
{
|
||||||
|
NSMutableArray<TagSprite*> *projectilesToDelete = new NSMutableArray<TagSprite*>;
|
||||||
|
NSMutableArray<TagSprite*>::NSMutableArrayIterator it, jt;
|
||||||
|
|
||||||
|
for (it = _projectiles->begin(); it != _projectiles->end(); it++)
|
||||||
|
{
|
||||||
|
TagSprite *projectile = *it;
|
||||||
|
|
||||||
|
CGRect projectileRect = CGRectMake(projectile->getPosition().x - (projectile->getContentSize().width/2),
|
||||||
|
projectile->getPosition().y - (projectile->getContentSize().height/2),
|
||||||
|
projectile->getContentSize().width,
|
||||||
|
projectile->getContentSize().height);
|
||||||
|
|
||||||
|
NSMutableArray<TagSprite*> *targetsToDelete = new NSMutableArray<TagSprite*>;
|
||||||
|
|
||||||
|
for (jt = _targets->begin(); jt != _targets->end(); jt++)
|
||||||
|
{
|
||||||
|
TagSprite *target = *jt;
|
||||||
|
|
||||||
|
CGRect targetRect = CGRectMake(target->getPosition().x - (target->getContentSize().width/2),
|
||||||
|
target->getPosition().y - (target->getContentSize().height/2),
|
||||||
|
target->getContentSize().width,
|
||||||
|
target->getContentSize().height);
|
||||||
|
|
||||||
|
if (CGRect::CGRectIntersectsRect(projectileRect, targetRect))
|
||||||
|
{
|
||||||
|
targetsToDelete->addObject(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (jt = targetsToDelete->begin(); jt != targetsToDelete->end(); jt++)
|
||||||
|
{
|
||||||
|
TagSprite *target = *jt;
|
||||||
|
|
||||||
|
_targets->removeObject(target);
|
||||||
|
this->removeChild(target, true);
|
||||||
|
_projectilesDestroyed++;
|
||||||
|
|
||||||
|
if (_projectilesDestroyed > 30)
|
||||||
|
{
|
||||||
|
GameOverScene *gameOverScene = GameOverScene::node();
|
||||||
|
gameOverScene->getLayer()->getLabel()->setString("You Win!");
|
||||||
|
CCDirector::getSharedDirector()->replaceScene(gameOverScene);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetsToDelete->count() > 0)
|
||||||
|
{
|
||||||
|
projectilesToDelete->addObject(projectile);
|
||||||
|
}
|
||||||
|
targetsToDelete->release();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (it = projectilesToDelete->begin(); it != projectilesToDelete->end(); it++)
|
||||||
|
{
|
||||||
|
TagSprite* projectile = *it;
|
||||||
|
|
||||||
|
_projectiles->removeObject(projectile);
|
||||||
|
this->removeChild(projectile, true);
|
||||||
|
}
|
||||||
|
projectilesToDelete->release();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HelloWorld::ccTouchesEnded(NSSet* touches, UIEvent* event)
|
||||||
|
{
|
||||||
|
// Choose one of the touches to work with
|
||||||
|
CCTouch* touch = (CCTouch*)( touches->anyObject() );
|
||||||
|
CGPoint location = touch->locationInView(touch->view());
|
||||||
|
location = CCDirector::getSharedDirector()->convertToGL(location);
|
||||||
|
|
||||||
|
// Set up initial location of projectile
|
||||||
|
CGSize winSize = CCDirector::getSharedDirector()->getWinSize();
|
||||||
|
TagSprite *projectile = (TagSprite*)CCSprite::spriteWithFile(IMG_PROJECTILE, CGRectMake(0, 0, 20, 20));
|
||||||
|
projectile->setPosition( ccp(20, winSize.height/2) );
|
||||||
|
|
||||||
|
// Determinie offset of location to projectile
|
||||||
|
float offX = location.x - projectile->getPosition().x;
|
||||||
|
float offY = location.y - projectile->getPosition().y;
|
||||||
|
|
||||||
|
// Bail out if we are shooting down or backwards
|
||||||
|
if (offX <= 0) return;
|
||||||
|
|
||||||
|
// Ok to add now - we've double checked position
|
||||||
|
this->addChild(projectile);
|
||||||
|
|
||||||
|
// Play a sound!
|
||||||
|
// SimpleAudioEngine->getSharedEngine()->playEffect("pew-pew-lei.caf");
|
||||||
|
|
||||||
|
// Determine where we wish to shoot the projectile to
|
||||||
|
float realX = winSize.width + (projectile->getContentSize().width / 2);
|
||||||
|
float ratio = (float)offY / (float)offX;
|
||||||
|
float realY = (realX * ratio) + projectile->getPosition().y;
|
||||||
|
CGPoint realDest = ccp(realX, realY);
|
||||||
|
|
||||||
|
// Determine the length of how far we're shooting
|
||||||
|
float offRealX = realX - projectile->getPosition().x;
|
||||||
|
float offRealY = realY - projectile->getPosition().y;
|
||||||
|
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
|
||||||
|
float velocity = 480/1; // 480pixels/1sec
|
||||||
|
float realMoveDuration = length/velocity;
|
||||||
|
|
||||||
|
// Move projectile to actual endpoint
|
||||||
|
projectile->runAction( CCSequence::actions(
|
||||||
|
CCMoveTo::actionWithDuration(realMoveDuration, realDest),
|
||||||
|
CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),
|
||||||
|
NULL) );
|
||||||
|
|
||||||
|
// Add to projectiles array
|
||||||
|
projectile->setTag(2);
|
||||||
|
_projectiles->addObject(projectile);
|
||||||
|
}
|
||||||
|
|
||||||
|
HelloWorld::HelloWorld()
|
||||||
|
:_targets(NULL)
|
||||||
|
,_projectiles(NULL)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// on "dealloc" you need to release all your retained objects
|
||||||
|
// cocos2d-x: destructor in cpp equals to dealloc in objc
|
||||||
|
HelloWorld::~HelloWorld()
|
||||||
|
{
|
||||||
|
if (_targets)
|
||||||
|
{
|
||||||
|
_targets->release();
|
||||||
|
_targets = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_projectiles)
|
||||||
|
{
|
||||||
|
_projectiles->release();
|
||||||
|
_projectiles = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void HelloWorld::menuCloseCallback(NSObject* pSender)
|
||||||
|
{
|
||||||
|
CCDirector::getSharedDirector()->end();
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
|
||||||
|
#ifndef _HELLOWORLD_LAYER_H_
|
||||||
|
#define _HELLOWORLD_LAYER_H_
|
||||||
|
|
||||||
|
#include "cocos2d.h"
|
||||||
|
|
||||||
|
class TagSprite : public cocos2d::CCSprite
|
||||||
|
{
|
||||||
|
CCX_SYNTHESIZE(int, tag, Tag);
|
||||||
|
};
|
||||||
|
|
||||||
|
class HelloWorld : public cocos2d::CCColorLayer
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
cocos2d::NSMutableArray<TagSprite*> *_targets;
|
||||||
|
cocos2d::NSMutableArray<TagSprite*> *_projectiles;
|
||||||
|
int _projectilesDestroyed;
|
||||||
|
|
||||||
|
public:
|
||||||
|
HelloWorld();
|
||||||
|
virtual ~HelloWorld();
|
||||||
|
|
||||||
|
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
|
||||||
|
virtual bool init();
|
||||||
|
|
||||||
|
void addTarget();
|
||||||
|
|
||||||
|
void ccTouchesEnded(cocos2d::NSSet* touches, cocos2d::UIEvent* event);
|
||||||
|
|
||||||
|
// callfunc callback
|
||||||
|
void spriteMoveFinished(cocos2d::CCNode* sender);
|
||||||
|
|
||||||
|
// scehdule selector, timer
|
||||||
|
void gameLogic(cocos2d::ccTime dt);
|
||||||
|
|
||||||
|
// a selector callback
|
||||||
|
void menuCloseCallback(cocos2d::NSObject* pSender);
|
||||||
|
|
||||||
|
// update
|
||||||
|
void update(cocos2d::ccTime dt);
|
||||||
|
|
||||||
|
static cocos2d::CCScene* HelloWorld::scene();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // _HELLOWORLD_SCENE_H_
|
|
@ -2,8 +2,9 @@
|
||||||
<VisualStudioProject
|
<VisualStudioProject
|
||||||
ProjectType="Visual C++"
|
ProjectType="Visual C++"
|
||||||
Version="9.00"
|
Version="9.00"
|
||||||
Name="cocosTemplate"
|
Name="Cocos2dSimpleGame"
|
||||||
ProjectGUID="{A2338E7B-155B-4A3B-AB4B-518ECDD26564}"
|
ProjectGUID="{A2338E7B-155B-4A3B-AB4B-518ECDD26564}"
|
||||||
|
RootNamespace="Cocos2dSimpleGame"
|
||||||
TargetFrameworkVersion="131072"
|
TargetFrameworkVersion="131072"
|
||||||
>
|
>
|
||||||
<Platforms>
|
<Platforms>
|
||||||
|
@ -64,7 +65,7 @@
|
||||||
<Tool
|
<Tool
|
||||||
Name="VCLinkerTool"
|
Name="VCLinkerTool"
|
||||||
AdditionalDependencies="WS2_32.Lib ..\..\PRJ_TG3\Common\SoftSupport\EosConfig.lib ..\..\PRJ_TG3\Common\SoftSupport\SoftSupport.lib ..\..\PRJ_TG3\Common\SoftSupport\TG3_DLL.lib"
|
AdditionalDependencies="WS2_32.Lib ..\..\PRJ_TG3\Common\SoftSupport\EosConfig.lib ..\..\PRJ_TG3\Common\SoftSupport\SoftSupport.lib ..\..\PRJ_TG3\Common\SoftSupport\TG3_DLL.lib"
|
||||||
OutputFile="$(OutDir)/cocosTemplate.dll"
|
OutputFile="$(OutDir)/Cocos2dSimpleGame.dll"
|
||||||
LinkIncremental="2"
|
LinkIncremental="2"
|
||||||
AdditionalLibraryDirectories="../../PRJ_TG3/Common/ICU/lib;../../PRJ_TG3/Mtapi/Win32/lib;../../PRJ_TG3/LIB/Win32Lib;../../PRJ_TG3/Common/SoftSupport"
|
AdditionalLibraryDirectories="../../PRJ_TG3/Common/ICU/lib;../../PRJ_TG3/Mtapi/Win32/lib;../../PRJ_TG3/LIB/Win32Lib;../../PRJ_TG3/Common/SoftSupport"
|
||||||
GenerateDebugInformation="true"
|
GenerateDebugInformation="true"
|
||||||
|
@ -184,27 +185,27 @@
|
||||||
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
|
||||||
>
|
>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\AppDelegate.cpp"
|
RelativePath=".\Cocos2dSimpleGameAppDelegate.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\AppDelegate.h"
|
RelativePath=".\Cocos2dSimpleGameAppDelegate.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\FirstLayer.cpp"
|
RelativePath=".\GameOverScene.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\FirstLayer.h"
|
RelativePath=".\GameOverScene.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\HelloWorld.cpp"
|
RelativePath=".\HelloWorldScene.cpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\HelloWorld.h"
|
RelativePath=".\HelloWorldScene.h"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
|
|
Loading…
Reference in New Issue