Skip to content

Commit

Permalink
20141229 Enemyの発生位置をランダム化
Browse files Browse the repository at this point in the history
  • Loading branch information
headrokoko committed Dec 29, 2014
1 parent e15eeef commit e0e962b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
66 changes: 50 additions & 16 deletions Classes/HelloWorldScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,19 @@ bool HelloWorld::init()
return false;
}

CCDictionary *spawnPoint = objectGroup->objectNamed("SpawnPoint");
CCDictionary* spawnPoint = objectGroup->objectNamed("SpawnPoint");

int x = ((CCString)*spawnPoint->valueForKey("x")).intValue();
int y = ((CCString)*spawnPoint->valueForKey("y")).intValue();

spawnpoint = ccp(x,y);
//CreatePlayer(ccp(x,y));
//CreateBomb(ccp(x,y));
//CreateBomb(spawnpoint);

this->setTouchEnabled(true);

//CreateObj().Create(this, _world, enemy, ccp(x,y) ); //オブジェクト生成クラスへenemy生成命令

scheduleUpdate(); //updateメソッドを実行
//this->schedule(schedule_selector(HelloWorld::EnemySpwan),1);

return true;
}
Expand All @@ -73,16 +72,11 @@ void HelloWorld::update(float dt)
{
_world->Step(dt,10,10);

//for(b2Body* subbody = _world->GetBodyList(); subbody; subbody = subbody->GetNext())
{
//if(subbody->GetUserData() != NULL)
{
//CCSprite* SP = (CCSprite*)subbody->GetUserData(); //P_bodyに格納されたuserdate(Sprite)の取り出し

//SP->setPosition(CCPointMake(P_body->GetPosition().x * PTM_RATIO, P_body->GetPosition().y * PTM_RATIO)); //P_bodyに割り当てられた画像の位置をP_bodyの位置に設定
}

}
}
void HelloWorld::EnemySpwan()
{
CCLog("test");
}

//背景設定メソッド
Expand All @@ -100,7 +94,7 @@ void HelloWorld::CreateBackground()

}

//Enemy作成
//Bomb作成
void HelloWorld::CreateBomb(CCPoint point)
{
PhysiSprite* BombSprite = new PhysiSprite();
Expand Down Expand Up @@ -132,6 +126,43 @@ void HelloWorld::CreateBomb(CCPoint point)

}

//Enemy作成
void HelloWorld::CreateEnemy(CCPoint point)
{
PhysiSprite* Enemyprite = new PhysiSprite();
Enemyprite->autorelease();
Enemyprite->initWithFile("Player.png");


float posX = (float)(rand()%(95-10+1)+10) / 100 ;

Enemyprite->setPosition(point);
this->addChild(Enemyprite);

b2BodyDef EnemyBodyDef;
EnemyBodyDef.type = b2_dynamicBody;
EnemyBodyDef.linearVelocity = b2Vec2(0.0f, 15.0f);
EnemyBodyDef.position.Set(ScreenSize.width * posX / PTM_RATIO, ScreenSize.height * 0.1f / PTM_RATIO);

EnemyBodyDef.userData = Enemyprite;

b2Body* Enemy_body = _world->CreateBody(&EnemyBodyDef);

b2CircleShape EnemyShape;
EnemyShape.m_radius = Enemyprite->getContentSize().width * 0.5 / PTM_RATIO;

b2FixtureDef BombFixDef;
BombFixDef.shape = &EnemyShape;
BombFixDef.density = 1.0f;
BombFixDef.friction = 0.9;

Enemy_body->CreateFixture(&BombFixDef);
Enemyprite->setPhysiBody(Enemy_body);

//this->setViewPointCenter(BombSprite->getPosition());

}

//プレイヤー作成
void HelloWorld::CreatePlayer(CCPoint point)
{
Expand Down Expand Up @@ -171,7 +202,7 @@ void HelloWorld::initPhysics()
_world = new b2World(gravity); //_worldにgravityを入力

//ゲーム画面端の設定
CCSize ScreenSize = CCDirector::sharedDirector()->getWinSize();
ScreenSize = CCDirector::sharedDirector()->getWinSize();
b2BodyDef worldBodyDef;
worldBodyDef.position.Set(0,0);

Expand Down Expand Up @@ -235,6 +266,7 @@ void HelloWorld::registerWithTouchDispatcher()
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}

//クリック押下イベント
bool HelloWorld::ccTouchBegan(CCTouch *touch, CCEvent *event)
{
CCDirector* pDirector = CCDirector::sharedDirector();
Expand All @@ -243,6 +275,8 @@ bool HelloWorld::ccTouchBegan(CCTouch *touch, CCEvent *event)
//CreatePlayer(touchPoint);

CreateBomb(touchPoint);
CreateEnemy(touchPoint);


return true;
}
Expand Down
12 changes: 10 additions & 2 deletions Classes/HelloWorldScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ USING_NS_CC;

class HelloWorld : public cocos2d::CCLayer
{

protected:

void EnemySpwan();

public:

~HelloWorld();
Expand All @@ -21,10 +26,11 @@ class HelloWorld : public cocos2d::CCLayer

virtual void CreateBackground(); //背景作成メソッド
virtual void CreatePlayer(CCPoint point); //プレイヤー作成メソッド
//virtual void CreateEnemy(CCPoint point); //Enemy作成メソッド
virtual void CreateEnemy(CCPoint point); //Enemy作成メソッド
virtual void CreateBomb(CCPoint point); //爆弾作成メソッド
virtual void draw(); //debugDraw作成メソッド 


void initPhysics(); //物理演算初期化

void update(float dt); //updateメソッド
Expand All @@ -47,6 +53,7 @@ class HelloWorld : public cocos2d::CCLayer
void ccTouchEnded(CCTouch *touch, CCEvent *event);

b2World* _world; //b2World用のフィールド宣言
CCSize ScreenSize;

CCSpriteBatchNode* ESpriteBatchNode;

Expand All @@ -61,7 +68,7 @@ class HelloWorld : public cocos2d::CCLayer

private:


CCPoint spawnpoint;
CCSpriteBatchNode* batchNode;
b2Body* P_body; //プレイヤーの物理ボディ

Expand All @@ -80,6 +87,7 @@ class HelloWorld : public cocos2d::CCLayer
HudLayer *_hud; //HUDレイヤー

int _numCollected;
int count;


};
Expand Down
Binary file modified Resources/Bomb.psd
Binary file not shown.
2 changes: 1 addition & 1 deletion Resources/Sample.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
</data>
</layer>
<objectgroup name="Objects">
<object name="SpawnPoint" x="12.3333" y="1036.33" width="605.667" height="53.3333"/>
<object name="SpawnPoint" x="64.3333" y="984.33" width="493.667" height="44"/>
</objectgroup>
</map>

0 comments on commit e0e962b

Please sign in to comment.