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);

    }

  • 相关阅读:
    centos 7 install
    sbt
    maven create project
    java异常个人理解
    (poj1094)Sorting It All Out
    stars
    Following Orders(拓扑排序)
    The House Of Santa Claus(dfs)
    Prime Path(bfs)
    Fence Repair(优先队列容器的应用)
  • 原文地址:https://www.cnblogs.com/daochong/p/5262379.html
Copyright © 2011-2022 走看看