SneakyButtonSkinnedBase *SneakyInputEx::buttonSkinnedBase(CCRect btnRect, const char *upSprite, const char *downSprite) { SneakyButtonSkinnedBase *buttonBase = new SneakyButtonSkinnedBase(); if (buttonBase) { buttonBase->autorelease(); buttonBase->init(); buttonBase->setDefaultSprite(CCSprite::spriteWithSpriteFrameName(upSprite)); buttonBase->setActivatedSprite(CCSprite::spriteWithSpriteFrameName(downSprite)); buttonBase->setPressSprite(CCSprite::spriteWithSpriteFrameName(downSprite)); SneakyButton *button = new SneakyButton(); if (button) { button->autorelease(); button->initWithRect(btnRect); button->setIsToggleable(false); button->setIsHoldable(true); buttonBase->setButton(button); return buttonBase; } buttonBase->release(); } return NULL; }
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; }
void InputLayer::addFireButton() { float buttonRadius = 50; Size screensize = Director::getInstance()->getVisibleSize(); fireButton = new SneakyButton(); fireButton->initWithRect(Rect::ZERO); fireButton->setIsHoldable(true); SneakyButtonSkinnedBase *skinFireButton = SneakyButtonSkinnedBase::create(); skinFireButton->setPosition(screensize.width - buttonRadius, buttonRadius); skinFireButton->setDefaultSprite(Sprite::create("button-default.png")); skinFireButton->setActivatedSprite(Sprite::create("button-default.png")); skinFireButton->setPressSprite(Sprite::create("button-pressed.png")); skinFireButton->setButton(fireButton); this->addChild(skinFireButton); }
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 Button::addFireButton() { //A¼ü float buttonRadius = 50; auto screenSize = Director::getInstance()->getWinSize(); fireButton = new SneakyButton(); fireButton->initWithRect(Rect(0, 0, buttonRadius, buttonRadius)); fireButton->setIsHoldable(true); //Ƥ·ô SneakyButtonSkinnedBase * skinFireButton = SneakyButtonSkinnedBase::create(); skinFireButton->setPosition(Vec2(screenSize.width - buttonRadius * 1.5f, buttonRadius * 1.5f)); skinFireButton->setDefaultSprite(Sprite::createWithSpriteFrameName("fire_button_default.png")); skinFireButton->setPressSprite(Sprite::createWithSpriteFrameName("fire_button_press.png")); skinFireButton->setActivatedSprite(Sprite::create("Icon.png")); skinFireButton->setDisabledSprite(Sprite::createWithSpriteFrameName("fire_button_default.png")); skinFireButton->setButton(fireButton); this->addChild(skinFireButton); }
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; }
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 }