25.cocos2d-x物理引擎(每点击一次屏幕添加一个小球)

HelloWorldScene.h文件:


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
USING_NS_CC;

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    //1.重写进入时的方法
    void onEnter();
    //2.给世界添加PhysicsBody
    void addEdges();
    //3.添加一个小球
    void addBall(float positionX,float positionY);
    void addBall(cocos2d::Vec2 position);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

private:
    Size visiableSize;


};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp文件:


#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    //1.有物理引擎世界的Scene
    auto scene = Scene::createWithPhysics();
    //2.输出全部的调试信息
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto rootNode = CSLoader::createNode("MainScene.csb");

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





    addChild(rootNode);

    return true;
}

void HelloWorld::onEnter()
{
    Layer::onEnter();

    addEdges();
    //正中间放置一个小球
    addBall(visiableSize.width / 2, visiableSize.height / 2);


    //3.添加点击监听事件
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [this](Touch *t, Event *e) {
        //4.点击位置添加一个小球
        this->addBall(t->getLocation());

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



}

void HelloWorld::addEdges()
{
    //宽高、设置边框大小、3
    auto body = PhysicsBody::createEdgeBox(visiableSize,PHYSICSBODY_MATERIAL_DEFAULT,3);
    auto edgeShape = Node::create();
    edgeShape->setPhysicsBody(body);
    edgeShape->setPosition(visiableSize.width/2, visiableSize.height/2);
    addChild(edgeShape);


}

void HelloWorld::addBall(float positionX, float positionY)
{
    auto ball = Sprite::create("ball1.png");
    ball->setPhysicsBody(PhysicsBody::createBox(ball->getContentSize()));
    ball->setPosition(positionX, positionY);
    addChild(ball);



}

void HelloWorld::addBall(cocos2d::Vec2 position)
{
    addBall(position.x, position.y);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值