8,303
社区成员
发帖
与我相关
我的任务
分享
//HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#define CC_ENABLE_BOX2D_INTEGRATION 1
#define CC_ENABLE_CHIPMUNK_INTEGRATION 0
#pragma once
#include "cocos2d.h"
#include "Box2D\Box2D.h"
#include "CCPhysicsSprite.h"
#define RATIO 48.0f
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
virtual void ccTouchesBegan(CCTouch *pTouch, CCEvent *pEvent);//----------------------监听单点触屏动作
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);//----------------------监听单点触屏动作
// virtual void onEnter();
// virtual void onExit();
virtual void update(float dt);
b2World * world;
cocos2d::extension::CCPhysicsSprite * Bird;
CCSize screenSize;
private:
void addBird();
void addGround();
void initWorld();
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
screenSize = CCDirector::sharedDirector()->getVisibleSize();
initWorld();
addBird();
addGround();
scheduleUpdate();
this->setTouchEnabled(true);//----------------------设置触屏可用
return true;
}
void HelloWorld::initWorld(){
world = new b2World(b2Vec2(0, -15));
}
void HelloWorld::update(float dt){
world->Step(dt, 8, 3);
}
void HelloWorld::addBird(){
Bird = cocos2d::extension::CCPhysicsSprite::create("bao.png");
CCSize size = Bird->getContentSize();
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(screenSize.width / 2 / RATIO, 6);
b2Body *birdBody = world->CreateBody(&bodyDef);
b2PolygonShape birdShape;
birdShape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef BirdFixtureDef;
BirdFixtureDef.shape = &birdShape;
birdBody->CreateFixture(&BirdFixtureDef);
Bird->setPTMRatio(RATIO);
Bird->setB2Body(birdBody);
CCLOG("add bird");
addChild(Bird);
}
void HelloWorld::addGround(){
cocos2d::extension::CCPhysicsSprite *ground = cocos2d::extension::CCPhysicsSprite::create("ground.png");
CCSize size = ground->getContentSize();
b2BodyDef bDef;
bDef.type = b2_staticBody;
bDef.position = b2Vec2(screenSize.width / 2 / RATIO, 1);
b2Body *groundBody = world->CreateBody(&bDef);
b2PolygonShape groundShape;
groundShape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundBody->CreateFixture(&groundFixtureDef);
ground->setB2Body(groundBody);
ground->setPTMRatio(RATIO);
addChild(ground);
CCLOG("add ground");
}
void HelloWorld::ccTouchesBegan(CCTouch *pTouch, CCEvent *pEvent){//----------------------监听触屏动作,但是不成功
Bird->getB2Body()->SetLinearVelocity(b2Vec2(0, 10));
CCLOG("touch");
}
void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){
}
/*
void HelloWorld::onEnter(){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
CCLayer::onEnter();
}
void HelloWorld::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
*/
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
void Player::onEnter()
{
Node::onEnter();
// Register Touch Event
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(Player::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(Player::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(Player::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}