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

前端之家收集整理的这篇文章主要介绍了25.cocos2d-x物理引擎(每点击一次屏幕添加一个小球)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

HelloWorldScene.h文件

#ifndef __HELLOWORLD_SCENE_H__@H_403_5@
#define@H_403_5@ __HELLOWORLD_SCENE_H__@H_403_5@

#include "cocos2d.h"@H_403_5@
USING_NS_CC;

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

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

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

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

private@H_403_5@:
    Size visiableSize;


};

#endif@H_403_5@ // __HELLOWORLD_SCENE_H__@H_403_5@

HelloWorldScene.cpp文件

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

USING_NS_CC;

using@H_403_5@ namespace cocostudio::timeline;

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

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

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

    // return the scene@H_403_5@
    return@H_403_5@ scene;
}

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

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

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





    addChild(rootNode);

    return@H_403_5@ true@H_403_5@;
}

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

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


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

        return@H_403_5@ false@H_403_5@;
    };
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this@H_403_5@);



}

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


}

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



}

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

猜你在找的Cocos2d-x相关文章