in the new version 2.0, I can not enter in update & touchended functions
thanks for the help…
#include “HelloWorldScene.h”
#include “SimpleAudioEngine.h”
#include “GameOverScene.h”
using namespace cocos2d;
using namespace CocosDenshion;
HelloWorld::~HelloWorld()
{
if (_targets)
{
_targets->release();
_targets = NULL;
}
if (_projectiles)
{
_projectiles->release();
_projectiles = NULL;
}
// cpp don't need to call super dealloc
// virtual destructor will do this
}
HelloWorld::HelloWorld()
:_targets(NULL)
,_projectiles(NULL)
,_projectilesDestroyed(0)
{
}
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(layer);
} while (0);
// return the scene
return scene;
}
void HelloWorld::draw()
{
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
glDisable(GL_TEXTURE_2D);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//world->DrawDebugData();
// restore default GL states
glEnable(GL_TEXTURE_2D);
//glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
// on “init” you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
this->setTouchEnabled(true);
// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);
/////////////////////////////
// 3. add your codes below...
//player
_projectilesDestroyed=0;
_targets=cocos2d::CCArray::create();
_projectiles=cocos2d::CCArray::create();
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *player = CCSprite::create("caon1.png",
CCRectMake(0, 30, 79, 40) );
player->setPosition( ccp(player->getContentSize().width/2, winSize.height/2) );
this->addChild(player);
this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 );
this->setTouchEnabled(true);
_targets = new CCArray();
_projectiles = new CCArray();
// use updateGame instead of update, otherwise it will conflit with SelectorProtocol::update
// see http://www.cocos2d-x.org/boards/6/topics/1478
this->schedule( schedule_selector(HelloWorld::updateGame) );
// CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(“background-music-aac.wav”, true);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//añadir enemigos…
void HelloWorld::addTarget()
{
CCSprite *target = CCSprite::create(“momia0001.png”,
CCRectMake(0,0,100,100));
// Determine where to spawn the target along the Y axis
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
float minY = target->getContentSize().height/2;
float maxY = winSize.height - target->getContentSize().height/2;
int rangeY = (int)(maxY - minY);
// srand( TimGetTicks() );
int actualY = ( rand() % rangeY ) + (int)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);
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::create(actualDuration, ccp(0 - target->getContentSize().width/2, actualY));
CCFiniteTimeAction* actionMoveDone= cocos2d::CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished));
target->runAction(CCSequence::create(actionMove, actionMoveDone, NULL));
// Add to targets array
target->setTag(1);
_targets->addObject(target);
}
//game over…
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *sprite = (CCSprite *)sender;
this->removeChild(sprite, true);
if (sprite->getTag() == 1) // target
{
_targets->removeObject(sprite);
GameOverScene *gameOverScene = GameOverScene::node();
gameOverScene->getLayer()->getLabel()->setString(“You Lose :[”);
CCDirector::sharedDirector()->replaceScene(gameOverScene);
}
else if (sprite->getTag() == 2) // projectile
{
_projectiles->removeObject(sprite);
}
}
//game logic
void HelloWorld::gameLogic(cocos2d::CCTime dt)
{
this->addTarget();
}
void HelloWorld::ccTouchEnded(CCSet* touches, CCEvent* event)
{
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCLog("++++++++after x:%f, y:%f", location.x, location.y);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png",
CCRectMake(0, 0, 20, 20));
projectile->setPosition( ccp(20, winSize.height/2) );
// 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 (offX <= 0) return;
// Ok to add now - we've double checked position
this->addChild(projectile);
// Determine where we wish to shoot the projectile to
int realX = winSize.width
+ (projectile->getContentSize().width/2);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + 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((offRealX * offRealX)
+ (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
/**/
// Move projectile to actual endpoint
projectile->runAction( CCSequence::create( CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
}
/void HelloWorld::ccTouchEnded(CCSet touches, CCEvent* event)
{
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCSetIterator it;
for (it = touches->begin(); it != touches->end(); it++) {
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->locationInView();
CCLog("++++++++befor x:%f, y:%f", location.x, location.y);
location = CCDirector::sharedDirector()->convertToGL(location);
CCLog("++++++++after x:%f, y:%f", location.x, location.y);
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::create(“Projectile.png”, CCRectMake(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);
// Determine where we wish to shoot the projectile to
float realX = winSize.width + (projectile->getContentSize().width/2);
float ratio = offY / offX;
float realY = (realX * ratio) + projectile->getPosition().y;
CCPoint 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;
//rotacion del player
CCPoint shootVector= ccpSub(location, projectile->getPosition());
CCFloat shootAngle = ccpToAngle(shootVector);
CCFloat cocosAngle = CC_RADIANS_TO_DEGREES(-1 * shootAngle);
CCFloat curAngle = player->getRotation();
CCFloat rotateDiff = cocosAngle - curAngle;
if (rotateDiff > 180)
rotateDiff -= 360;
if (rotateDiff < -180)
rotateDiff += 360;
CCFloat rotateSpeed = 0.5 / 180; // Would take 0.5 seconds to rotate half a circle
CCFloat rotateDuration = fabs(rotateDiff * rotateSpeed);
// Move player slightly backwards
CCPoint position = ccpAdd(player->getPosition(), ccp(-10, 0));
// CCFiniteTimeAction* actionMBy = CCMoveBy::create(0.1, position);
CCFiniteTimeAction* actionRoto =CCRotateTo::create(rotateDuration, cocosAngle);
//cambiar SPF x finishShot
CCFiniteTimeAction* actCalc = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished));
player->runAction(CCSequence::create(actionRoto,actCalc));
// Move projectile to actual endpoint
projectile->runAction( CCSequence::create( CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished))));
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(“pew-pew-lei.wav”);
}
}*/
// cpp with cocos2d-x
/void HelloWorld::ccTouchBegan(CCSet touches, CCEvent* event)
{
// Choose one of the touches to work with
CCTouch* touch = (CCTouch*)( touches->anyObject() );
CCPoint location = touch->locationInView();
CCLog("++++++++befor x:%f, y:%f", location.x, location.y);
location = CCDirector::sharedDirector()->convertToGL(location);
CCLog("++++++++after x:%f, y:%f", location.x, location.y);
// Set up initial location of projectile
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *projectile = CCSprite::create("arrow.png", CCRectMake(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);
// Determine where we wish to shoot the projectile to
float realX = winSize.width + (projectile->getContentSize().width/2);
float ratio = offY / offX;
float realY = (realX * ratio) + projectile->getPosition().y;
CCPoint 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::create( CCMoveTo::create(realMoveDuration, realDest),
CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished))));
// Add to projectiles array
projectile->setTag(2);
_projectiles->addObject(projectile);
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
}*/
void HelloWorld::updateGame(cocos2d::CCTime dt)
{
//CCMutableArray<CCSprite*> projectilesToDelete = new CCMutableArray<CCSprite>;
CCArray projectilesToDelete = new CCArray();
//CCSetIterator it,jt;
CCObject arrayItem;
CCObject* arrayItem2;
CCARRAY_FOREACH(_projectiles, arrayItem)
{
CCSprite* pItem = (CCSprite*)(arrayItem);
CCRect projectileRect = CCRectMake(
pItem->getPosition().x - (pItem->getContentSize().width/2),
pItem->getPosition().y - (pItem->getContentSize().height/2),
pItem->getContentSize().width,
pItem->getContentSize().height);
CCArray *targetsToDelete = new CCArray();
CCARRAY_FOREACH(_targets, arrayItem2)
{
CCSprite* pItem2 = (CCSprite*)(arrayItem2);
CCRect targetRect = CCRectMake(
pItem2->getPosition().x - (pItem2->getContentSize().width/2),
pItem2->getPosition().y - (pItem2->getContentSize().height/2),
pItem2->getContentSize().width,
pItem2->getContentSize().height);
if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
{
targetsToDelete->addObject(pItem2);
}
}
CCARRAY_FOREACH(targetsToDelete, arrayItem2)
{
CCSprite *target =(CCSprite*)(arrayItem2);
_targets->removeObject(target);
this->removeChild(target, true);
_projectilesDestroyed++;
if (_projectilesDestroyed >= 5)
{
GameOverScene *gameOverScene = GameOverScene::node();
gameOverScene->getLayer()->getLabel()->setString("You Win!");
CCDirector::sharedDirector()->replaceScene(gameOverScene);
}
}
if (targetsToDelete->count() > 0)
{
projectilesToDelete->addObject(pItem);
}
targetsToDelete->release();
}
CCARRAY_FOREACH(projectilesToDelete, arrayItem)
{
CCSprite* pItem = (CCSprite*)(arrayItem);
_projectiles->removeObject(pItem);
this->removeChild(pItem, true);
}
projectilesToDelete->release();
}
void HelloWorld::registerWithTouchDispatcher()
{
// CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}