码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d-x step by step(3) Double Kill

时间:2015-04-25 16:32:20      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈!

技术分享

这就是最终界面了,没画一个球形  就用一个白色方框代替吧。

啊哈!

public:
    virtual bool init();

    static cocos2d::Scene* scene();
    void update(float dt) override;

    void menuCloseCallback(Ref* sender);

    void onButtonClicked(Ref *pSender,ui::Widget::TouchEventType type);
    void onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event);
    void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
    CREATE_FUNC(FishTestScence);

首先呢,我们需要keypreed 和keyreleased 两个事件,因为要触发键盘事件

 

 

bool isUp = false;
bool isLeft = true;
bool isPreessLeft = false;
bool isPressRight = false;
bool isDead = true;
void FishTestScence::update(float dt)
{
    if (isDead)return;

    const cocos2d::Vector<cocos2d::Node*> arr = this->getChildren();
    Sprite* sprite1 = (Sprite*)arr.at(2);
    Sprite* sprite2 = (Sprite*)arr.at(3);

    if (ballPosition.x < 5)
    {
        isLeft = false;
    }
    else if (ballPosition.x > maxWidth)
    {
        isLeft = true;
    }

    if (ballPosition.y < 36)
    {
        isUp = true;
    }
    else if (ballPosition.y > maxHeight)
    {
        isUp = false;
    }

    isLeft ? ballPosition.x -= 2 : ballPosition.x += 2;
    isUp ? ballPosition.y += 2 : ballPosition.y -= 2;

    if (isPreessLeft)dangbanVec.x -= 2;
    if (isPressRight)dangbanVec.x += 2;

    if (((ballPosition.x - dangbanVec.x) > 45 && ballPosition.y <= 36) || ((ballPosition.x - dangbanVec.x) < -45 && ballPosition.y <= 36)) isDead = true;

    sprite1->setPosition(Vec2(dangbanVec.x, dangbanVec.y));

    sprite2->setPosition(Vec2(ballPosition.x, ballPosition.y));

}

// 键位响应函数原型
void FishTestScence::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
    const cocos2d::Vector<cocos2d::Node*> arr = this->getChildren();
    Sprite* sprite1 = (Sprite*)arr.at(2);
    Vec2 vec = sprite1->getPosition();
    switch (keyCode)
    {
    case     EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        isPreessLeft = true;
        break;
    case     EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        isPressRight = true;
        break;

    default:
        break;
    }
}

void FishTestScence::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event)
{
    switch (keyCode)
    {
    case     EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        isPreessLeft = false;;
        break;
    case     EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        isPressRight = false;
        break;
    case     EventKeyboard::KeyCode::KEY_UP_ARROW:
        isDead = false;
        break;
    default:
        break;
    }
}

这是代码实现,大概是这样的,当点击up箭头,游戏开始,当白色方块不在触板内,则game over ,用 isDead缓存

再次按up重新开始,

小东西,没什么技术含量,只是娱乐!

不写了,代码里有!

    // create a scene. it‘s an autorelease object
    auto scene = HelloWorld::scene();
    auto fish = FishTestScence::scene();
    // run
    director->runWithScene(fish);

这里替换一下就行,图片如果没有的话 上一篇内有呢

 

烦死了!

妈蛋的淘宝,给姑娘买东西半天提示我重置密码和支付密码,老子重置了,浪费老子一个小时,还你妈比的不能买,qnmlgb!艹!

 

代码:源代码

 

cocos2d-x step by step(3) Double Kill

标签:

原文地址:http://www.cnblogs.com/fish124423/p/4455982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!