Skip to content

Instantly share code, notes, and snippets.

@kazukitanaka0611
Created September 22, 2013 07:11
Show Gist options
  • Save kazukitanaka0611/6657486 to your computer and use it in GitHub Desktop.
Save kazukitanaka0611/6657486 to your computer and use it in GitHub Desktop.
this sample http://tf.hateblo.jp/entry/2013/04/10/000119 cocos2d-x version
#include "cocos2d.h"
using namespace cocos2d;
class MyBullet : public cocos2d::CCNode
{
public:
static MyBullet* create();
static MyBullet* create(CCPoint position, CCPoint vector, float speed, CCSpriteFrame *spriteFrame);
public:
MyBullet();
virtual ~MyBullet();
virtual bool init();
virtual bool init(CCPoint position, CCPoint vector, float speed, CCSpriteFrame *spriteFrame);
void update(float delta);
};
#endif
#include "MyBullet.h"
using namespace cocos2d;
MyBullet::MyBullet()
{
}
MyBullet::~MyBullet()
{
}
MyBullet* MyBullet::create()
{
MyBullet* pRet = new MyBullet();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool MyBullet::init()
{
return true;
}
MyBullet* MyBullet::create(CCPoint position, CCPoint vector, float speed, CCSpriteFrame *spriteFrame)
{
MyBullet* pRet = new MyBullet();
if (pRet && pRet->init(position, vector, speed, spriteFrame))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool MyBullet::init(CCPoint position, CCPoint vector, float speed, CCSpriteFrame *spriteFrame)
{
this->setPosition(position);
CCSprite *sprite = CCSprite::createWithSpriteFrame(spriteFrame);
this->addChild(sprite);
CCPoint uniVector = ccpNormalize(vector);
CCPoint zeroDegree = ccp(1, 0);
float bulletAngle = ccpAngleSigned(uniVector, zeroDegree);
this->setRotation(90.0 + CC_RADIANS_TO_DEGREES(bulletAngle));
CCPoint velocity = ccpMult(uniVector, speed);
CCMoveBy *moveBy = CCMoveBy::create(1.0, velocity);
CCRepeatForever *repeatForever = CCRepeatForever::create(moveBy);
this->runAction(repeatForever);
this->scheduleUpdate();
return true;
}
void MyBullet::update(float delta)
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCRect screenRect = CCRectMake(0, 0, screenSize.width, screenSize.height);
if (screenRect.intersectsRect(this->boundingBox()) == false)
{
this->removeFromParentAndCleanup(true);
}
}
#include "cocos2d.h"
using namespace cocos2d;
class MySprite : public cocos2d::CCNode
{
public:
static MySprite* create();
static MySprite* createWithImage();
public:
MySprite();
virtual ~MySprite();
virtual bool init();
virtual bool initWithImage();
void shoot();
private:
CCSprite *_sprite;
CCSprite *_option;
};
#endif
#include "MySprite.h"
#include "MyBullet.h"
using namespace cocos2d;
MySprite::MySprite()
{
}
MySprite::~MySprite()
{
}
MySprite* MySprite::create()
{
MySprite* pRet = new MySprite();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool MySprite::init()
{
return true;
}
MySprite* MySprite::createWithImage()
{
MySprite* pRet = new MySprite();
if (pRet && pRet->initWithImage())
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool MySprite::initWithImage()
{
_sprite = CCSprite::create("Icon-72.png");
this->addChild(_sprite);
_option = CCSprite::create("Icon-72.png");
this->addChild(_option);
_option->setOpacity(255 * 0.5);
CCSize screen = CCDirector::sharedDirector()->getWinSize();
this->setPosition(ccp(screen.width / 2, screen.height / 2));
_sprite->setPosition(CCPointZero);
_option->setPosition(ccpAdd(_sprite->getPosition(), ccp(_sprite->getContentSize().width * 2.0 ,0)));
this->setContentSize(_sprite->getContentSize());
CCRotateBy *roteteby = CCRotateBy::create(2, 360);
CCScaleBy *scaleUp = CCScaleBy::create(1, 2.0);
CCScaleBy *scaleDown = CCScaleBy::create(1, 1.0/2.0);
CCSequence *sequence = CCSequence::create(roteteby, scaleUp, scaleDown, NULL);
CCRepeatForever *repeat = CCRepeatForever::create(sequence);
this->runAction(repeat);
this->schedule(schedule_selector(MySprite::shoot), 0.1);
return true;
}
void MySprite::shoot()
{
CCPoint screenCoordinate = ccpRotateByAngle(ccpAdd(this->getPosition(),
ccpMult(_option->getPosition(), this->getScale())),
this->getPosition(),
CC_DEGREES_TO_RADIANS(-this->getRotation()));
CCPoint vector = ccpSub(screenCoordinate, this->getPosition());
float speed = 200;
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("Icon-Small.png");
CCSize texSize = texture->getContentSize();
CCRect texRect = CCRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(texture, texRect);
MyBullet *bullet = MyBullet::create(screenCoordinate, vector, speed, frame);
this->getParent()->addChild(bullet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment