bool PlayLayer::init() { if(!BaseLayer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); MenuItemFont *back = MenuItemFont::create("back", CC_CALLBACK_1(PlayLayer::back, this)); Menu *menu = Menu::create(back, NULL); menu->setPosition(visibleSize.width*9/10, visibleSize.height*9/10); this->addChild(menu); SpriteFrameCache* cache = SpriteFrameCache::getInstance(); cache->addSpriteFramesWithFile("UI.plist", "UI.png"); mJoystick = NULL; mJoystick = new SneakyJoystick(); mJoystick->initWithRect(Rect::ZERO); mJoystick->setAutoCenter(true); mJoystick->setHasDeadzone(true); mJoystick->setDeadRadius(10); SneakyJoystickSkinnedBase* jstickSkin = new SneakyJoystickSkinnedBase(); jstickSkin->autorelease(); jstickSkin->init(); jstickSkin->setBackgroundSprite(CCSprite::createWithSpriteFrameName("JoyStick-base.png")); jstickSkin->setThumbSprite(CCSprite::createWithSpriteFrameName("JoyStick-thumb.png")); //jstickSkin->getThumbSprite()->setScale(0.5f); jstickSkin->setPosition(visibleSize.width*1/10, visibleSize.width*1/10); jstickSkin->setJoystick(mJoystick); this->addChild(jstickSkin); mButtonA = NULL; mButtonA = new SneakyButton(); mButtonA->initWithRect(Rect::ZERO); mButtonA->setIsToggleable(false); mButtonA->setIsHoldable(true); SneakyButtonSkinnedBase* btnASkin = new SneakyButtonSkinnedBase(); btnASkin->autorelease(); btnASkin->init(); btnASkin->setPosition(visibleSize.width*9/10, visibleSize.width*1/10); btnASkin->setDefaultSprite(CCSprite::createWithSpriteFrameName("button-default.png")); btnASkin->setPressSprite(CCSprite::createWithSpriteFrameName("button-pressed.png")); btnASkin->setActivatedSprite(CCSprite::createWithSpriteFrameName("button-activated.png")); //btnASkin->setDisabledSprite(CCSprite::createWithSpriteFrameName("button-disabled.png")); btnASkin->setButton(mButtonA); this->addChild(btnASkin); this->schedule(schedule_selector(PlayLayer::inputUpdate)); startPlay(); return true; }
HudLayer::HudLayer() { CCMenuItemSprite* btn_pause = CCMenuItemSprite::create( CCSprite::createWithSpriteFrameName("CloseNormal.png"), CCSprite::createWithSpriteFrameName("CloseSelected.png"), this, menu_selector(HudLayer::menuPauseCallback)); btn_pause->setPosition(CCPointZero); CCMenu* menu_pause = CCMenu::createWithItem(btn_pause); menu_pause->setPosition(ccp(PAUSE_MENU_POS_X, PAUSE_MENU_POS_Y)); this->addChild(menu_pause); mJoystick = NULL; mJoystick = new SneakyJoystick(); mJoystick->initWithRect(CCRectZero); mJoystick->setAutoCenter(true); mJoystick->setHasDeadzone(true); mJoystick->setDeadRadius(JOYSTICK_DEAD_RADIUS); SneakyJoystickSkinnedBase* jstickSkin = new SneakyJoystickSkinnedBase(); jstickSkin->autorelease(); jstickSkin->init(); jstickSkin->setBackgroundSprite(CCSprite::createWithSpriteFrameName("JoyStick-base.png")); jstickSkin->setThumbSprite(CCSprite::createWithSpriteFrameName("JoyStick-thumb.png")); //jstickSkin->getThumbSprite()->setScale(0.5f); jstickSkin->setPosition(ccp(JOYSTICK_POS_X, JOYSTICK_POS_Y)); jstickSkin->setJoystick(mJoystick); this->addChild(jstickSkin); mButtonA = NULL; mButtonA = new SneakyButton(); mButtonA->initWithRect(CCRectZero); mButtonA->setIsToggleable(false); mButtonA->setIsHoldable(true); SneakyButtonSkinnedBase* btnASkin = new SneakyButtonSkinnedBase(); btnASkin->autorelease(); btnASkin->init(); btnASkin->setPosition(ccp(BTN_A_POS_X, BTN_A_POS_Y)); btnASkin->setDefaultSprite(CCSprite::createWithSpriteFrameName("button-default.png")); btnASkin->setPressSprite(CCSprite::createWithSpriteFrameName("button-pressed.png")); btnASkin->setActivatedSprite(CCSprite::createWithSpriteFrameName("button-activated.png")); //btnASkin->setDisabledSprite(CCSprite::createWithSpriteFrameName("button-disabled.png")); btnASkin->setButton(mButtonA); this->addChild(btnASkin); init(); }
void InputLayer::addJoyStick() { float stickRadius = 50; joystick = new SneakyJoystick(); joystick->initWithRect(CCRectMake(0, 0, 100.0f, 100.0f)); joystick->setAutoCenter(true); joystick->setHasDeadzone(true); joystick->setDeadRadius(10); SneakyJoystickSkinnedBase *skinjoystick = SneakyJoystickSkinnedBase::create(); skinjoystick->setPosition(stickRadius * 1.5f, stickRadius * 1.5f); skinjoystick->setBackgroundSprite(Sprite::create("button-disabled.png")); skinjoystick->getBackgroundSprite()->setColor(ccMAGENTA); skinjoystick->setThumbSprite(Sprite::create("button-disabled.png")); skinjoystick->getThumbSprite()->setScale(0.5f); skinjoystick->setJoystick(joystick); this->addChild(skinjoystick); }
// on "init" you need to initialize your instance bool JoystickController::init(CCNode *parent) { bool bRet = false; do { ////////////////////////////////////////////////////////////////////////// // super init first ////////////////////////////////////////////////////////////////////////// CC_BREAK_IF(! Controller::init()); SneakyJoystickSkinnedBase *joystickBase = SneakyInputEx::joystickSkinnedBase(CCRect(0, 0, 64, 64), "circleBig.png", "circleSmall.png"); if (joystickBase) { joystickBase->setPosition(System::PointMake(48, 48)); mJoystick = joystickBase->getJoystick(); parent->addChild(joystickBase); } SneakyButtonSkinnedBase *buttonBase = SneakyInputEx::buttonSkinnedBase(CCRect(0,0,32,32), "buttonBlue.png", "buttonOrange.png"); if (buttonBase) { buttonBase->setPosition(System::PointMake(480-48, 32)); mButton = buttonBase->getButton(); parent->addChild(buttonBase); } mButtonDown = false; bRet = true; } while (0); return bRet; }
void Button::addJoystick() { //ÐéÄâÊÖ±ú //·½Ïò¼ü float stickRadius = 50; joystick = new SneakyJoystick(); joystick->initWithRect(Rect(0, 0, stickRadius, stickRadius)); joystick->autorelease(); joystick->setAutoCenter(true); joystick->setIsDPad(true); joystick->setNumberOfDirections(360); //Ƥ·ô SneakyJoystickSkinnedBase *skinStick = SneakyJoystickSkinnedBase::create(); skinStick->setPosition(stickRadius * 1.5f, stickRadius * 1.5f); skinStick->setBackgroundSprite(Sprite::createWithSpriteFrameName("control_bg.png")); skinStick->setThumbSprite(Sprite::createWithSpriteFrameName("cen.png")); skinStick->setJoystick(joystick); this->addChild(skinStick); }
HudLayer::HudLayer() { CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("UI.plist", "UI.png"); mJoystick = NULL; mJoystick = new SneakyJoystick(); mJoystick->initWithRect(CCRectZero); mJoystick->setAutoCenter(true); mJoystick->setHasDeadzone(true); mJoystick->setDeadRadius(10); SneakyJoystickSkinnedBase* jstickSkin = new SneakyJoystickSkinnedBase(); jstickSkin->autorelease(); jstickSkin->init(); jstickSkin->setBackgroundSprite(CCSprite::createWithSpriteFrameName("JoyStick-base.png")); jstickSkin->setThumbSprite(CCSprite::createWithSpriteFrameName("JoyStick-thumb.png")); //jstickSkin->getThumbSprite()->setScale(0.5f); jstickSkin->setPosition(ccp(50, 50)); jstickSkin->setJoystick(mJoystick); this->addChild(jstickSkin); mButtonA = NULL; mButtonA = new SneakyButton(); mButtonA->initWithRect(CCRectZero); mButtonA->setIsToggleable(false); mButtonA->setIsHoldable(true); SneakyButtonSkinnedBase* btnASkin = new SneakyButtonSkinnedBase(); btnASkin->autorelease(); btnASkin->init(); btnASkin->setPosition(ccp(430, 50)); btnASkin->setDefaultSprite(CCSprite::createWithSpriteFrameName("button-default.png")); btnASkin->setPressSprite(CCSprite::createWithSpriteFrameName("button-pressed.png")); btnASkin->setActivatedSprite(CCSprite::createWithSpriteFrameName("button-activated.png")); //btnASkin->setDisabledSprite(CCSprite::createWithSpriteFrameName("button-disabled.png")); btnASkin->setButton(mButtonA); this->addChild(btnASkin); init(); }
// on "init" you need to initialize your instance bool CCGVirtualJoystick::init() { ////////////////////////////// // 1. super init first if (!Layer::init()) { return false; } // set joystick params. float defaultThumbRadius = 64; float defaultBackgroundRadius = 100; float defaultDeathRadius = 10; Vec2 defaultJoystickPosition = Vec2(200, 200); _leftJoystick = static_cast<CCGJoystick*>(new SneakyJoystick()); _leftJoystick->initWithRect(Rect::ZERO); _leftJoystick->setAutoCenter(true); _leftJoystick->setHasDeadzone(true); _leftJoystick->setDeadRadius(defaultDeathRadius); SneakyJoystickSkinnedBase *leftJoystickSkin = new SneakyJoystickSkinnedBase(); leftJoystickSkin->autorelease(); leftJoystickSkin->init(); // set background sprite leftJoystickSkin->setBackgroundSprite(CCSprite::create("joystick/RadialJoy_Area.png")); auto backgroundSpriteSize = leftJoystickSkin->getBackgroundSprite()->getContentSize(); leftJoystickSkin->getBackgroundSprite()->setScaleX(2* defaultBackgroundRadius / backgroundSpriteSize.width); leftJoystickSkin->getBackgroundSprite()->setScaleY(2* defaultBackgroundRadius / backgroundSpriteSize.height); // set thumb sprite leftJoystickSkin->setThumbSprite(CCSprite::create("joystick/Button_normal.png")); auto thumbSpriteSize = leftJoystickSkin->getThumbSprite()->getContentSize(); leftJoystickSkin->getThumbSprite()->setScaleX(2 * defaultThumbRadius / thumbSpriteSize.width); leftJoystickSkin->getThumbSprite()->setScaleY(2 * defaultThumbRadius / thumbSpriteSize.height); leftJoystickSkin->setPosition(defaultJoystickPosition); leftJoystickSkin->setJoystick(_leftJoystick.get()); this->addChild(leftJoystickSkin); // create attack button Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 defaultAttackButtonPosition = Vec2(visibleSize.width - 200, 200); _attackButton = static_cast<CCGButton*>(new SneakyButton()); _attackButton->initWithRect(Rect::ZERO); _attackButton->setIsToggleable(false); _attackButton->setIsHoldable(false); SneakyButtonSkinnedBase *attackButtonSkin = new SneakyButtonSkinnedBase(); attackButtonSkin->autorelease(); attackButtonSkin->init(); attackButtonSkin->setDefaultSprite(CCSprite::create("joystick/attackButton_normal.png")); attackButtonSkin->setPressSprite(CCSprite::create("joystick/attackButton_pressed.png")); attackButtonSkin->setActivatedSprite(CCSprite::create("joystick/attackButton_pressed.png")); attackButtonSkin->setPosition(defaultAttackButtonPosition); attackButtonSkin->setButton(_attackButton.get()); this->addChild(attackButtonSkin); return true; }
HelloWorld::HelloWorld() { hello = this; // Define the gravity vector. b2Vec2 gravity; gravity.Set(0.0f, 0.0f); // Do we want to let bodies sleep? bool doSleep = true; // Construct a world object, which will hold and simulate the rigid bodies. world = new b2World(gravity); world->SetAllowSleeping(doSleep); world->SetContinuousPhysics(true); joyStick = new SneakyJoystick(); joyStick->autorelease(); joyStick->initWithRect(CCRectZero); joyStick->setAutoCenter(true); joyStick->setHasDeadzone(true); joyStick->setDeadRadius(10); SneakyJoystickSkinnedBase *joystickSkin = new SneakyJoystickSkinnedBase(); joystickSkin->autorelease(); joystickSkin->init(); joystickSkin->setBackgroundSprite(CCSprite::create("button-default.png")); joystickSkin->setThumbSprite(CCSprite::create("button-disabled.png")); joystickSkin->getThumbSprite()->setScale(0.5f); joystickSkin->setPosition(ccp(50, 50)); joystickSkin->setJoystick(joyStick); // load shapes GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("physicalTest.plist"); setTouchEnabled(true); setAccelerometerEnabled( true ); CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); // m_debugDraw = new GLESDebugDraw( PTM_RATIO ); // world->SetDebugDraw(m_debugDraw); // // uint32 flags = 0; // flags += b2Draw::e_shapeBit; // flags += b2Draw::e_jointBit; // // flags += b2Draw::e_aabbBit; // // flags += b2Draw::e_pairBit; // // flags += b2Draw::e_centerOfMassBit; // m_debugDraw->SetFlags(flags); // b2BodyDef groundBodyDef; groundBodyDef.position.Set(screenSize.width/2/PTM_RATIO, screenSize.height/2/PTM_RATIO); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. b2Body *groundBody = world->CreateBody(&groundBodyDef); // Define the ground box shape. b2PolygonShape groundBox; /* // bottom groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, -screenSize.height/2/PTM_RATIO), 0); groundBody->CreateFixture(&groundBox, 0); // top groundBox.SetAsBox(screenSize.width/2/PTM_RATIO, 0, b2Vec2(0, screenSize.height/2/PTM_RATIO), 0); groundBody->CreateFixture(&groundBox, 0); // left groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(-screenSize.width/2/PTM_RATIO, 0), 0); groundBody->CreateFixture(&groundBox, 0); // right groundBox.SetAsBox(0, screenSize.height/2/PTM_RATIO, b2Vec2(screenSize.width/2/PTM_RATIO, 0), 0); groundBody->CreateFixture(&groundBox, 0); */ //initBG initBG(); PhysicsSprite *sprite = PhysicsSprite::create("missile", CCPointMake(screenSize.width/2/32, screenSize.height/2/32), world); sprite->setCollisionGroup(-10); sprite->setTag(1); playerBody = sprite->getBody(); bgLayer->addChild(sprite); Weapon *gun = Weapon::create("SlienceBullet.png"); sprite->addChild(gun); PhysicsSprite *sprite_2 = PhysicsSprite::create("missile", CCPointMake(screenSize.width/2/32, screenSize.height/2/32), world); sprite_2->setTag(2); bgLayer->addChild(sprite_2); //addNewSpriteWithCoords( CCPointMake(screenSize.width/2, screenSize.height/2) ); //playerBody = addNewSpriteWithCoords( CCPointMake(screenSize.width/2, screenSize.height/2),"missile" ,1); //addNewSpriteWithCoords( CCPointMake(screenSize.width/2 + 100, screenSize.height/2),"missile",2 ); CCFollow *follow = new CCFollow(); follow->initWithTarget((CCSprite*)playerBody->GetUserData(),CCRectMake(0, 0, screenSize.width * 2, screenSize.height * 2)); bgLayer->runAction(follow); bgLayer->setTag(100); follow->release(); schedule(schedule_selector(HelloWorld::tick)); scheduleUpdate(); CCLayer *UI = CCLayer::create(); this->addChild(UI); UI->addChild(joystickSkin); joystickSkin->setPosition(ccp(100, 100)); speed = 0; // MyContactListener *_contactListener; _contactListener = new MyContactListener(); world->SetContactListener(_contactListener); }
void JoystickLayer::generateJoystick() { Rect joystickBaseDimensions(0, 0, 6.0f, 6.0f); Point joystickBasePosition(SCREEN_W * 0.21f, SCREEN_H * 0.2f); Sprite* bg = Sprite::createWithSpriteFrameName("background.png"); Sprite* thumb = Sprite::createWithSpriteFrameName("thumb.png"); bg->setScale(0.37f); thumb->setScale(0.45f); SneakyJoystickSkinnedBase* joystickBase = new SneakyJoystickSkinnedBase(); _base = joystickBase; joystickBase->init(); joystickBase->setPosition(joystickBasePosition); joystickBase->setBackgroundSprite(bg); joystickBase->setThumbSprite(thumb); _joystick = new SneakyJoystick(); _joystick->initWithRect(joystickBaseDimensions); _joystick->autorelease(); joystickBase->setJoystick(_joystick); joystickBase->setPosition(joystickBasePosition); Rect jumpButtonDimensions(0, 0, 64.0f, 64.0f); Point jumpButtonPosition; jumpButtonPosition = ccp(SCREEN_W * 0.8f, SCREEN_H * 0.2f); SneakyButtonSkinnedBase *jumpButtonBase = new SneakyButtonSkinnedBase(); jumpButtonBase->init(); jumpButtonBase->setPosition(jumpButtonPosition); Sprite* button1 = Sprite::createWithSpriteFrameName("jumpButton.png"); button1->setScale(0.6f); jumpButtonBase->setDefaultSprite(button1); jumpButtonBase->setActivatedSprite(button1); jumpButtonBase->setDisabledSprite(button1); jumpButtonBase->setPressSprite(button1); _jumpButton = new SneakyButton(); _jumpButton->initWithRect(jumpButtonDimensions); _jumpButton->autorelease(); jumpButtonBase->setButton(_jumpButton); jumpButtonBase->setPosition(jumpButtonPosition); _jumpButton->retain(); Rect attackButtonDimensions(0, 0, 64.0f, 64.0f); Point attackButtonPosition; attackButtonPosition = ccp(SCREEN_W * 0.93f, SCREEN_H * 0.3f); SneakyButtonSkinnedBase *attackButtonBase = new SneakyButtonSkinnedBase(); attackButtonBase->init(); attackButtonBase->setPosition(attackButtonPosition); Sprite* button2 = Sprite::createWithSpriteFrameName("attackButton.png"); button2->setScale(0.6f); attackButtonBase->setDefaultSprite(button2); attackButtonBase->setActivatedSprite(button2); attackButtonBase->setDisabledSprite(button2); attackButtonBase->setPressSprite(button2); _attackButton = new SneakyButton(); _attackButton->initWithRect(attackButtonDimensions); _attackButton->autorelease(); attackButtonBase->setButton(_attackButton); attackButtonBase->setPosition(attackButtonPosition); _attackButton->retain(); this->addChild(attackButtonBase); this->addChild(jumpButtonBase); this->addChild(joystickBase); scheduleUpdate(); this->setTouchEnabled(true); }
void GameControlLayer::initJoystickAndButtons() { CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); CCRect joystickBaseDimensions = CCRectMake(0, 0, 128.0f, 128.0f); CCRect jumpButtonDimensions = CCRectMake(0, 0, 64.0f, 64.0f); CCRect attackButtonDimensions = CCRectMake(0, 0, 64.0f, 64.0f); CCPoint joystickBasePosition; CCPoint jumpButtonPosition; CCPoint attackButtonPosition; joystickBasePosition = ccp(screenSize.width*0.07f, screenSize.height*0.11f); jumpButtonPosition = ccp(screenSize.width*0.93f, screenSize.height*0.11f); // Set the attackButtonPosition to be 80 points above the jump button // So that their positions are fixed and do not change because of different screen resolutions attackButtonPosition = ccp(screenSize.width*0.93f, screenSize.height*0.11f + 80.0f); //Joystick SneakyJoystickSkinnedBase *joystickBase = SneakyJoystickSkinnedBase::node(); joystickBase->setPosition(joystickBasePosition); joystickBase->setBackgroundSprite(CCSprite::spriteWithFile("dpadDown.png")); joystickBase->setThumbSprite(CCSprite::spriteWithFile("joystickDown.png")); SneakyJoystick *aJoystick = new SneakyJoystick(); aJoystick->initWithRect(joystickBaseDimensions); aJoystick->autorelease(); joystickBase->setJoystick(aJoystick); /* IMPORTANT: do not need to retain here like in the book, because the setJoystick method does retain as well In the book the setJoystick method is not called, instead the joystick is set directly, which we can't do in cocos2d-x Same thing applies for the buttons below */ //joystickBase->getJoystick()->retain(); leftJoystick = joystickBase->getJoystick(); this->addChild(joystickBase); //Jump button SneakyButtonSkinnedBase *jumpButtonBase = SneakyButtonSkinnedBase::node(); jumpButtonBase->setPosition(jumpButtonPosition); // 13 jumpButtonBase->setDefaultSprite(CCSprite::spriteWithFile("jumpUp.png")); // 14 jumpButtonBase->setActivatedSprite(CCSprite::spriteWithFile("jumpDown.png")); // 15 jumpButtonBase->setPressSprite(CCSprite::spriteWithFile("jumpDown.png")); // 16 SneakyButton *aButton = new SneakyButton(); aButton->initWithRect(jumpButtonDimensions); aButton->autorelease(); jumpButtonBase->setButton(aButton); // 17 //jumpButtonBase->getButton()->retain(); // 18 jumpButton = jumpButtonBase->getButton(); jumpButton->setIsToggleable(false); // 19 this->addChild(jumpButtonBase); // 20 //Attack button SneakyButtonSkinnedBase *attackButtonBase = SneakyButtonSkinnedBase::node(); attackButtonBase->setPosition(attackButtonPosition); // 13 attackButtonBase->setDefaultSprite(CCSprite::spriteWithFile("handUp.png")); // 14 attackButtonBase->setActivatedSprite(CCSprite::spriteWithFile("handDown.png")); // 15 attackButtonBase->setPressSprite(CCSprite::spriteWithFile("handDown.png")); // 16 SneakyButton *aButton1 = new SneakyButton(); aButton1->initWithRect(attackButtonDimensions); aButton1->autorelease(); attackButtonBase->setButton(aButton1); // 17 //attackButtonBase->getButton()->retain(); // 18 attackButton = attackButtonBase->getButton(); attackButton->setIsToggleable(false); // 19 this->addChild(attackButtonBase); // 20 }