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

    }

  • 相关阅读:
    瀑布流布局(等宽不等高jQuery)
    多组图自动无限循环(swiper轮播)
    $.ajax的标准写法
    走马灯特效
    如何使用js改变HTML中title里面固定的文字
    test
    单例设计模式
    线程的五种状态(线程的生命周期)
    Net基础班第十三天
    装箱、拆箱
  • 原文地址:https://www.cnblogs.com/daochong/p/5262379.html
Copyright © 2011-2022 走看看