思路:就是先构建一个多场景控制的场景,然后把在场景上添加两个层,一个是Userinterface层,另一个是gamescene层,两个层都可以接受触摸事件,在gamescene层上添加精灵类,蜘蛛类继承与精灵类,太阳类也是继承ccsprite类,蜘蛛类由于要实现接受触摸事件,因而还要继承public CCTargetedTouchDelegate协议,剩下的事情就是实现各自的动画效果。具体代码如下:
#ifndef __helloworld__spiders__
#define __helloworld__spiders__
#include "cocos2d.h"
using namespace cocos2d;
#include
class Spiders :public CCSprite,public CCTargetedTouchDelegate
{
public:
bool init();
void initspiders(CCLayerColor*node);
void update(float dt);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void myspidermove();
virtual bool isTouchForme(CCPoint location);
CREATE_FUNC(Spiders);
};
#endif
#include "spiders.h"
bool Spiders ::init()
{
if(!CCSprite::init())
{
return false;
}
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
//
this->scheduleUpdate();
return true;
}
void Spiders::initspiders(CCLayerColor*node)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
this->initWithFile("playe2r.png");
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
this->setPosition(location);
node->addChild(this,1);
}
void Spiders::update(float dt)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
if(this->numberOfRunningActions()==0)
{
CCMoveTo *action = CCMoveTo::create(1.2, location);
this->runAction(action);
}
}
bool Spiders ::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint location = pTouch->getLocation();
bool toucheme = this->isTouchForme(location);
if (toucheme)
{
CCScaleBy* s = CCScaleBy::create(0.5, 2);
CCCallFunc * t = CCCallFunc::create(this, callfunc_selector(Spiders::myspidermove));
CCSequence *action = CCSequence::create(s,s->reverse(),t,NULL);
this->runAction(action);
}
return toucheme;
}
void Spiders::myspidermove()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
CCMoveTo *action = CCMoveTo::create(1.2, location);
this->runAction(action);
}
bool Spiders ::isTouchForme(CCPoint location)
{
bool contain = this->boundingBox().containsPoint(location);
return contain;
}
#ifndef __helloworld__sun__
#define __helloworld__sun__
#include "cocos2d.h"
#include "CCTouchDispatcher.h"
using namespace cocos2d;
#include
class Sun:public CCSprite
{
private:
CCSprite * sun;
int vecloty;
public:
bool init();
void update(float dt);
~Sun();
CREATE_FUNC(Sun);
};
#endif
#include "sun.h"
#include "CCScheduler.h"
bool Sun::init()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
if (!CCSprite::init())
{
return false;
}
vecloty = 0;
sun = CCSprite::create("sun.png");
sun->setPosition(ccp(sun->getContentSize().width/2,size.height-sun->getContentSize().height/2));
this->addChild(sun);
this->schedule(schedule_selector(Sun::update),0.05f);
return true;
}
void Sun::update(float dt)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
if(sun->numberOfRunningActions()==0)
{
CCMoveTo *action = CCMoveTo::create(1.2, location);
sun->runAction(action);
}
}
Sun::~Sun()
{
}
#ifndef __helloworld__Multilayerscene__
#define __helloworld__Multilayerscene__
#include "cocos2d.h"
#include "spider.h"
#include "userinterface.h"
using namespace cocos2d;
#include
class Multilayerscene:CCNode
{
private:
static Multilayerscene * multilayerscene;
public:
bool init();
static CCScene * scene();
static Multilayerscene*shared();
Userinterface* getuserinterface();
Gamescene*getspider();
CREATE_FUNC(Multilayerscene);
};
#endif
#include "Multilayerscene.h"
Multilayerscene* Multilayerscene::multilayerscene = NULL;
bool Multilayerscene::init()
{
if (!CCNode::init())
{
return false;
}
multilayerscene = this;
Gamescene * spide = Gamescene::create();
spide->setTag(100);
this->addChild(spide,1);
Userinterface * interface = Userinterface::create();
interface->setTag(99);
this->addChild(interface,2);
return true;
}
Multilayerscene* Multilayerscene::shared()
{
return multilayerscene;
}
CCScene * Multilayerscene::scene()
{
CCScene * scene = CCScene::create();
Multilayerscene * layer = Multilayerscene::create();
scene->addChild(layer);
return scene;
}
Userinterface* Multilayerscene::getuserinterface()
{
CCNode * layer = this->getChildByTag(99);
return (Userinterface*)layer;
}
Gamescene* Multilayerscene::getspider()
{
CCNode * layer = this->getChildByTag(100);
return (Gamescene*)layer;
}
#ifndef __helloworld__userinterface__
#define __helloworld__userinterface__
#include "cocos2d.h"
#include "spider.h"
using namespace cocos2d;
#include
class Userinterface:public CCLayer
{
private:
CCSprite * gamescene;
public:
bool init();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void registerWithTouchDispatcher(void);
virtual bool isTouchForme(CCPoint location);
CREATE_FUNC(Userinterface);
};
#endif
#include "Multilayerscene.h"
#include "userinterface.h"
bool Userinterface::init()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
if (!CCLayer::init())
{
return false;
}
this->setTouchEnabled(true);
gamescene = CCSprite::create("pic11.png");
gamescene->setPosition(ccp(size.width/2,size.height-gamescene->getContentSize().height/2));
this->addChild(gamescene);
return true;
}
bool Userinterface::isTouchForme(CCPoint location)
{
CCRect spriterect = gamescene->boundingBox();
bool contain = spriterect.containsPoint(location);
return contain;
}
bool Userinterface::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint location = pTouch->getLocation();
bool touchme = this->isTouchForme(location);
if (touchme)
{
this->gamescene->setScale(1.5f);
Gamescene * spide = Multilayerscene::shared()->getspider();
CCScaleTo * s = CCScaleTo::create(1.5, 0.05);
CCScaleTo * t = CCScaleTo::create(1.5, 1);
CCRotateBy * rotate = CCRotateBy::create(1.2, 90);
CCSequence * sequence = CCSequence::create(s,t,rotate,rotate->reverse(),NULL);
spide->runAction(sequence);
}
return touchme;
}
void Userinterface::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
//gamescene->setColor(ccWHITE);
this->gamescene->setScale(1);
}
void Userinterface::registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, true);
}
#ifndef __helloworld__spider__
#define __helloworld__spider__
#include "cocos2d.h"
#include "mylpayer.h"
#include "SimpleAudioEngine.h"
#include "sun.h"
#include "spiders.h"
#include
using namespace cocos2d;
class Gamescene:public CCLayerColor
{
private:
Sun * sun;
CCSprite *myspider;
CCSprite *enemyspider;
CCArray *enemys;
int playervelocity;
int numspidersmove;
float spidemovedutration;
int score;
CCLabelTTF *label;
public:
bool init();
// static CCScene*scene();
// void spritemove();
// void addsprite();
// void update();
//
// void updateenemy();
void update(float dt);
void initspiders();
void restspiders();
void spidersupdate();
void runspidermovesequence(CCSprite*spide);
void spiderbelowscreen(CCSprite*spide);
void updateenemyspiders();
void setspiderposition(CCSprite*object);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual bool isTouchForme(CCPoint location);
virtual void registerWithTouchDispatcher(void);
virtual void onEnter();
virtual void onExit();
virtual void onEnterTransitionDidFinish();
CREATE_FUNC(Gamescene);
};
#endif
#include "spider.h"
#include "spider.h"
bool Gamescene::init()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
if (!CCLayerColor::init())
{
return false;
}
playervelocity = 0;
score = 0;
label = CCLabelTTF::create("0", "Thonburi", 60);
label->setColor(ccc3(0, 255, 0));
label->setPosition(ccp(size.width/2, size.height/2));
this->addChild(label,1);
this->setTouchEnabled(true);
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("laser_ship.caf");
enemys = CCArray::create();
enemys->retain();
myspider = CCSprite::create("playe2r.png");
myspider->setPosition(ccp(size.width/2, myspider->getContentSize().height/2));
this->addChild(myspider,1);
sun = Sun::create();
this->addChild(sun,1);
for(int i =0;i<5;i++)
{
Spiders * t = Spiders::create();
t->initspiders(this);
}
CCSprite * s = CCSprite::create("progress.png");
CCSprite * t = CCSprite::create("progresss.png");
CCProgressTimer * ps = CCProgressTimer::create(t);
ps->setType(kCCProgressTimerTypeRadial);
ps->setPosition(size.width-ps->getContentSize().width/2, size.height-ps->getContentSize().height/2);
ps->setPercentage(0);
ps->setTag(121);
this->addChild(ps,2);
CCProgressTimer * progerss = CCProgressTimer::create(s);
progerss->setType(kCCProgressTimerTypeBar);
progerss->setPosition(size.width/2, size.height/2+progerss->getContentSize().height*2);
progerss->setPercentage(0);
progerss->setTag(1111);
this->addChild(progerss,2);
CCLayerColor * color = CCLayerColor::create(ccc4(255, 255, 255, 255));
this->addChild(color,0);
//this->initspiders();
this->scheduleUpdate();
return true;
}
void Gamescene::update(float dt)
{
static int j=0;
CCProgressTimer * time=(CCProgressTimer *)this->getChildByTag(1111);
CCProgressTimer * timer = (CCProgressTimer *)this->getChildByTag(121);
if(time->getPercentage()==100)
{
j=1;
}
else if(time->getPercentage()==0)
{
j = 0;
}
if(j==0)
{
timer->setPercentage(timer->getPercentage()+dt*10);
time->setPercentage(time->getPercentage()+dt*10);
}
else
{
timer->setPercentage(timer->getPercentage()-dt*10);
time->setPercentage(time->getPercentage()-dt*10);
}
static int i =0;
CCSize size = CCDirector::sharedDirector()->getWinSize();
if(i==0)
{
playervelocity++;
}
else
{
playervelocity--;
}
CCPoint pos = myspider->getPosition();
float imagewidth = myspider->getContentSize().width*0.5f;
float leftbundle = imagewidth;
float rightbundle = size.width-imagewidth;
pos.x+=playervelocity*0.1*dt;
if(pos.x<leftbundle)
{
pos.x = leftbundle;
playervelocity =0;
i=0;
}
else if(pos.x>rightbundle)
{
pos.x = rightbundle;
playervelocity =0;
i=1;
}
myspider->setPosition(pos);
// this->updateenemyspiders();
}
bool Gamescene:: ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint location = pTouch->getLocation();
bool toucheme = this->isTouchForme(location);
CCPoint real = ccpSub(location, this->getPosition());
float l = sqrtf(powf(real.x, 2)+powf(real.y, 2));
CCMoveBy * action = CCMoveBy::create(l/200, location);
CCSequence* sequence = CCSequence::create(action,action->reverse(),NULL);
if (toucheme)
{
this->runAction(sequence);
}
return toucheme;
}
void Gamescene:: ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
this->stopAllActions();
CCPoint location = pTouch->getLocation();
this->setPosition(location);
}
void Gamescene:: ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
CCMoveTo * action = CCMoveTo::create(1.2,ccp(0,0));
this->runAction(action);
}
bool Gamescene:: isTouchForme(CCPoint location)
{
return this->boundingBox().containsPoint(location);
}
void Gamescene:: registerWithTouchDispatcher(void)
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
#ifndef __helloworld__spiders__
#define __helloworld__spiders__
#include "cocos2d.h"
using namespace cocos2d;
#include <iostream>
class Spiders :public CCSprite,public CCTargetedTouchDelegate
{
public:
bool init();
void initspiders(CCLayerColor*node);
void update(float dt);
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void myspidermove();
virtual bool isTouchForme(CCPoint location);
~Spiders();
CREATE_FUNC(Spiders);
};
#endif
#include "spiders.h"
bool Spiders ::init()
{
if(!CCSprite::init())
{
return false;
}
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, -1, true);
//
this->scheduleUpdate();
return true;
}
void Spiders::initspiders(CCLayerColor*node)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
this->initWithFile("playe2r.png");
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
this->setPosition(location);
node->addChild(this,1);
}
void Spiders::update(float dt)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
if(this->numberOfRunningActions()==0)
{
CCMoveTo *action = CCMoveTo::create(1.2, location);
this->runAction(action);
}
}
bool Spiders ::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint location = pTouch->getLocation();
bool toucheme = this->isTouchForme(location);
if (toucheme)
{
CCScaleBy* s = CCScaleBy::create(0.5, 2);
CCCallFunc * t = CCCallFunc::create(this, callfunc_selector(Spiders::myspidermove));
CCSequence *action = CCSequence::create(s,s->reverse(),t,NULL);
this->runAction(action);
}
return toucheme;
}
void Spiders::myspidermove()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
float maxX = size.width-this->getContentSize().width*0.5f;
float minx = this->getContentSize().width*0.5f;
float maxY = size.height-this->getContentSize().height*0.5f;
float minY = this->getContentSize().height*0.5;
CCPoint location = CCPointMake(CCRANDOM_0_1()*(maxX-minx), CCRANDOM_0_1()*(maxY-minY));
CCMoveTo *action = CCMoveTo::create(1.2, location);
this->runAction(action);
}
bool Spiders ::isTouchForme(CCPoint location)
{
bool contain = this->boundingBox().containsPoint(location);
return contain;
}
Spiders::~Spiders()
{
this->unscheduleUpdate();
CCDirector::sharedDirector()->getTouchDispatcher()->removeAllDelegates();
}