zoukankan      html  css  js  c++  java
  • cocos2d-x之物理引擎初试

    发现问题:监听代码部分不能在onEnter()段书写

    bool HelloWorld::init()

    {

        if ( !Layer::init() )

        {

            return false;

        }

        

        visibleSize=Director::getInstance()->getVisibleSize();

        

        

        auto listener=EventListenerTouchOneByOne::create();

        listener->onTouchBegan = [this](Touch *t,Event *){

            this->addBall(t->getLocation());

            

            return false;

        };

        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);

        return true;

    }

    void HelloWorld::addBall(float positionX,float positionY){

        auto b=Sprite::create("GOODS_3201_65.png");

        b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));

        b->setPosition(positionX,positionY);

        addChild(b);

    }

    void HelloWorld::onEnter(){

        Layer::onEnter();

        addBall(visibleSize.width/2,visibleSize.height/2);

        addEdges();

        

    }

    void HelloWorld::addBall(Vec2 position){

        addBall(position.x,position.y);

    }

    void HelloWorld::addEdges(){

        auto body=PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3);

        

        auto edgeShape=Node::create();

        edgeShape->setPhysicsBody(body);

        edgeShape->setPosition(visibleSize.width/2,visibleSize.height/2);

        addChild(edgeShape);

    }

  • 相关阅读:
    nginx主配置文件详解
    微信网页第三方登录原理
    QQ第三方登陆流程详解
    php垃圾回收机制
    mysql索引
    MySQL性能优化的最佳20+条经验
    MYSQL explain详解
    mysql分区功能详细介绍,以及实例
    MySQL分表、分区
    Redis主从读写分离配置
  • 原文地址:https://www.cnblogs.com/daochong/p/5262379.html
Copyright © 2011-2022 走看看