SneakyJoystickSkinnedBase *SneakyInputEx::joystickSkinnedBase(CCRect padRect, const char *bgSprite, const char *thumbSprite) { SneakyJoystickSkinnedBase *joystickBase = new SneakyJoystickSkinnedBase(); if (joystickBase) { joystickBase->autorelease(); joystickBase->init(); joystickBase->setBackgroundSprite(CCSprite::spriteWithSpriteFrameName(bgSprite)); joystickBase->setThumbSprite(CCSprite::spriteWithSpriteFrameName(thumbSprite)); SneakyJoystick *joystick = new SneakyJoystick(); if (joystick) { joystick->autorelease(); joystick->initWithRect(padRect); joystickBase->setJoystick(joystick); return joystickBase; } joystickBase->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; }
NS_CC_BEGIN SneakyJoystickSkinnedBase* SneakyJoystickSkinnedBase::create ( KDvoid ) { SneakyJoystickSkinnedBase* pRet = new SneakyJoystickSkinnedBase ( ); if ( pRet && pRet->init ( ) ) { pRet->autorelease ( ); } else { CC_SAFE_DELETE ( pRet ); } return pRet; }
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(); }
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); }