cocos2dx 로 간단 한 단기 어로 달인 게임 (4)
7427 단어 cocos2dx
#ifndef _Bullet_H_
#define _Bullet_H_
#include"cocos2d.h"
using namespace cocos2d;
enum BULLETTYPE
{
BulletTypeOne,
BulletTypeTwo
};
class Bullet:public cocos2d::Sprite
{
public:
Bullet(BULLETTYPE t);
~Bullet();
bool init() override;
static Bullet*create(BULLETTYPE t);
//
void setDir(float dir)
{
this->dir = dir;
}
//
BULLETTYPE getBulletType()
{
return this->type;
}
void onEnter() override;
void onExit() override;
void setLive(bool isLive)
{
this->isLive = isLive;
}
double getAtt()
{
return att;
}
int getCostCoin()
{
return this->costCoin;
}
void bulletBack();//
void DeadCartoonOne();//
void DeadCartoonTwo();//
bool koufei;// gamelayer
private:
BULLETTYPE type;//
void update(float dt);//
float dir;//
double att;//
bool isLive;//
int speed;//
int costCoin;//
};
#endif // !_Bullet_H_
bullet.cpp
#include "Bullet.h"
#include"BulletManage.h"
Bullet::Bullet(BULLETTYPE t):
type(t)
{
switch (type)
{
case BulletTypeOne:
att = 4;//
costCoin = 7;//
break;
case BulletTypeTwo:
att = 5;//
costCoin = 1;//
break;
default:
break;
}
speed = 700;//
}
Bullet::~Bullet()
{
}
bool Bullet::init()
{
switch (type)
{
case BulletTypeOne:
if (!initWithFile("/bullet/bullet1.png"))
{
return false;
}
break;
case BulletTypeTwo:
if (!initWithFile("/bullet/bullet2.png"))
{
return false;
}
break;
default:
break;
}
//
scheduleUpdate();
koufei=false;// gamelayer
return true;
}
Bullet * Bullet::create(BULLETTYPE t)
{
Bullet*ret = new(std::nothrow) Bullet(t);
if (ret&&ret->init())
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
return ret;
}
void Bullet::onEnter()
{
Node::onEnter();//
koufei = false;// gamelayer
//
switch (type)
{
case BulletTypeOne:
this->costCoin = 7;
this->att = 7;
break;
case BulletTypeTwo:
this->costCoin = 1;
this->att = 5;
break;
default:
break;
}
//
scheduleUpdate();
isLive = true;
}
void Bullet::onExit()
{
Node::onExit();
}
void Bullet::bulletBack()
{
BulletManage::getInstance()->collection(this);
}
void Bullet::DeadCartoonOne()
{
// 0
this->costCoin = 0;
// 0
this->att = 0;
//1 animation-ref
Animation* act = Animation::create();
//2
act->addSpriteFrameWithFile("/bullet/bullet1bom.png");
//3
act->setDelayPerUnit(0.3);//
act->setLoops(1);// ,-1
act->setRestoreOriginalFrame(true);//
//4.Animation Animate ( Animate)
Animate* animate = Animate::create(act);
CallFunc* callFunc = CallFunc::create(this,
callfunc_selector(Bullet::bulletBack));//
Sequence* seqAct = Sequence::create(animate, callFunc, nullptr);// :... , nullptr
this->runAction(seqAct);
}
void Bullet::DeadCartoonTwo()
{
// 0
this->costCoin = 0;
// 0
this->att = 0;
//1 animation-ref
Animation* act = Animation::create();
//2
act->addSpriteFrameWithFile("/bullet/bullet2bom.png");
//3
act->setDelayPerUnit(0.3);//
act->setLoops(1);// ,-1
act->setRestoreOriginalFrame(true);//
//4.Animation Animate ( Animate)
Animate* animate = Animate::create(act);
CallFunc* callFunc = CallFunc::create(this,
callfunc_selector(Bullet::bulletBack));//
Sequence* seqAct = Sequence::create(animate, callFunc, nullptr);// :... , nullptr
this->runAction(seqAct);
}
void Bullet::update(float dt)
{
if (!isLive)//
{
switch (type)
{
case BulletTypeOne:
DeadCartoonOne();//
break;
case BulletTypeTwo:
DeadCartoonTwo();//
break;
default:
break;
}
return;
}
Vec2 pos = getPosition();//
Size winSize = Director::getInstance()->getWinSize();//
/* */
if ( getPositionX()<0 || getPositionX()>winSize.width||getPositionY()<0)
{
this->dir = -(dir);//
this->setRotation(dir * 180 / 3.14159);//
}
else if(getPositionY()>winSize.height)//
{
if (dir>=0)
{
this->dir = dir + 3.14159 / 2;//
this->setRotation(dir * 180 / 3.14159);//
}
else if(dir<0)
{
this->dir = dir - 3.14159 / 2;//
this->setRotation(dir * 180 / 3.14159);//
}
}else if(getPositionY()<=20)//
{
if (dir>=0)
{
this->dir = dir - 3.14159 / 2;//
this->setRotation(dir * 180 / 3.14159);//
}
else if (dir<0)
{
this->dir = dir + 3.14159 / 2;//
this->setRotation(dir * 180 / 3.14159);//
}
}
//
Vec2 Dir = (Vec2(sin(dir), cos(dir)));
setPosition(getPosition() + Dir *dt*speed);
}
bulletFactory.h
#ifndef _BulletFactory_H_
#define _BulletFactory_H_
#include "Bullet.h"
class BulletFactory
{
public:
BulletFactory();
~BulletFactory();
static Bullet*create(BULLETTYPE type);
private:
};
#endif // !1
bulletFactory.cpp
#include "BulletFactory.h"
#include"BulletManage.h"
BulletFactory::BulletFactory()
{
}
BulletFactory::~BulletFactory()
{
}
Bullet * BulletFactory::create(BULLETTYPE type)
{
//
Bullet*pBullet = nullptr;
//
pBullet = BulletManage::getInstance()->findDeath(type);
if (pBullet==nullptr)
{
//
pBullet = Bullet::create(type);
}
// +1,
pBullet->retain();
//
BulletManage::getInstance()->add(pBullet);
return pBullet;
}
bulletManage.h
#ifndef _BulletManage_H_
#define _BulletManage_H_
#include
class Bullet;
enum BULLETTYPE;
class BulletManage
{
public:
~BulletManage();
static BulletManage*getInstance()
{
if (pInstance == nullptr)
{
pInstance = new BulletManage;
}
return pInstance;
}
void add(Bullet*bullet);//
void collection(Bullet*bullet);//
//
Bullet*findDeath(BULLETTYPE type);
public:
BulletManage();
static BulletManage* pInstance;
std::list BulletLive;//
std::listBulletDead;//
};
#endif // !_BulletManage_H_
bulletManage.cpp
#include "BulletManage.h"
#include"Bullet.h"
BulletManage*BulletManage::pInstance = nullptr;
BulletManage::~BulletManage()
{
//
for (Bullet*bullet : BulletLive)
bullet->release();
//
BulletDead.clear();
for (Bullet*bullet : BulletDead)
bullet->release();
BulletDead.clear();
}
void BulletManage::add(Bullet * bullet)
{
BulletLive.push_back(bullet);//
}
void BulletManage::collection(Bullet * bullet)
{
bullet->removeFromParent();//
BulletLive.remove(bullet);//
BulletDead.push_back(bullet);//
}
Bullet * BulletManage::findDeath(BULLETTYPE type)
{
for (Bullet*bullet:BulletDead)
{
if (type==bullet->getBulletType())
{
BulletDead.remove(bullet);
return bullet;
}
}
return nullptr;
}
BulletManage::BulletManage()
{
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cocos2dx3.0 게임 프로그래밍 3 - 액션 게임 같은 대상의 상호작용게임 대상과 대상 간의 상호작용을 피할 수 없다. 예를 들어 게이머가 적을 공격하고 부피가 충돌하는 검측 등이다. 처음에 깃털을 떨어뜨리는 것은 가장 둔한 방법이다. 바로 클래스에 다른 종류의 지침을 저장하는 것이다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.