#include "PlayScene.h" #include "spritesheet.hpp" using namespace TexturePacker; USING_NS_CC; Scene* Play::createScene() { // 'scene' is an autorelease object auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setGravity( Vec2( 0, -300 ) ); auto layer = Play::create(); layer->SetPhysicsWorld( scene->getPhysicsWorld() ); scene->addChild(layer); return scene; } bool Play::init() { if ( !Layer::init() ) { return false; } if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255) )) { return false; } Spritesheet::addSpriteFramesToCache(); visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto edgeBody = PhysicsBody::createEdgeBox( visibleSize, PhysicsMaterial(0,0,0) ); auto edgeNode = Node::create(); edgeNode->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y ) ); edgeNode->setPhysicsBody( edgeBody ); this->addChild( edgeNode ); auto idle = SpriteFrameCache::getInstance()->getSpriteFrameByName(Spritesheet::idle); idle_air = SpriteFrameCache::getInstance()->getSpriteFrameByName(Spritesheet::idle_air); idle_air->setAnchorPoint(Vec2(0, 0)); idle->setAnchorPoint(Vec2(0, 0)); auto splayer = Sprite::createWithSpriteFrame(idle); splayer->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height/2 + splayer->getContentSize().height)); cocos2d::Size size1={90.0f,90.0f}; spriteBody = PhysicsBody::createBox( size1,PhysicsMaterial(0.05f,0,0) ); splayer->setPhysicsBody( spriteBody ); this->addChild(splayer, 0); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Play::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(Play::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(Play::onTouchEnded, this); listener->onTouchCancelled = CC_CALLBACK_2(Play::onTouchCancelled, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); isTouchDown = false; initialTouchPos[0] = 0; initialTouchPos[1] = 0; this->scheduleUpdate(); return true; } void Play::update(float delta){ idle_air->setAnchorPoint(Vec2(0, 0)); auto velocity = spriteBody->getVelocity(); auto scene =Director::getInstance() ->getRunningScene(); if (isTouchDown == true) { if (initialTouchPos[0] - currentTouchPos[0] > visibleSize.width * 0.05) { spriteBody->resetForces(); spriteBody->setVelocity(Vec2(0,0)); scene->getPhysicsWorld()->setGravity( Vec2( 0, 0) ); spriteBody ->applyImpulse(Vec2(-50000,0)); scene->getPhysicsWorld()->setGravity( Vec2( 0, -300 ) ); isTouchDown = false; } else if (initialTouchPos[0] - currentTouchPos[0] < - visibleSize.width * 0.05) { spriteBody ->applyImpulse(Vec2(50000,0)); isTouchDown = false; } else if (initialTouchPos[1] - currentTouchPos[1] > visibleSize.width * 0.05) { spriteBody->resetForces(); spriteBody->setVelocity(Vec2(0,0)); spriteBody ->applyImpulse(Vec2(0,-15000)); isTouchDown = false; } else if (initialTouchPos[1] - currentTouchPos[1] < - visibleSize.width * 0.05) { splayer->setSpriteFrame(idle_air); isTouchDown = false; } } } bool Play::onTouchBegan(Touch *touch, Event *event) { initialTouchPos[0] = touch->getLocation().x; initialTouchPos[1] = touch->getLocation().y; currentTouchPos[0] = touch->getLocation().x; currentTouchPos[1] = touch->getLocation().y; isTouchDown = true; return true; } void Play::onTouchMoved(Touch *touch, Event *event) { currentTouchPos[0] = touch->getLocation().x; currentTouchPos[1] = touch->getLocation().y; } void Play::onTouchEnded(Touch *touch, Event *event) { isTouchDown = false; } void Play::onTouchCancelled(Touch *touch, Event *event) { onTouchEnded(touch, event); }