Cocos2d-x 物理场景简单搭建

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

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

USING_NS_CC;

class HelloWorld : public cocos2d::Layer
{
private:
	Vector<Sprite *>SP;
	Sprite *touchedS;
public:
    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();

	virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event);
	virtual void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event);
	virtual void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event);
	void addNewSpriteAtPosition(cocos2d::Vec2 p);

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

#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"



using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics(); //创建物理场景
	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); 
	scene->getPhysicsWorld()->setGravity(Vec2(0,-100));  //设置重力方向
    // '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;
    }
	Vec2 location;
	touchedS = nullptr;

	location.x =  100 + 50;
	location.y = 500;
	addNewSpriteAtPosition(location); //首先创建一个精灵
	

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	//定义世界的边界
	auto body = PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT, 1.0f); 
		auto edgeNode = Node::create();
		edgeNode->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
		edgeNode->setPhysicsBody(body);
		this->addChild(edgeNode);
		setTouchEnabled(true); 
		//设置为单点触摸
		setTouchMode(Touch::DispatchMode::ONE_BY_ONE);

    return true;
}
bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
	auto location = touch->getLocation();
	for (auto sp : SP)
	{
		if (sp->getBoundingBox().containsPoint(location))
		{
			touchedS = sp;
			//touchedS->getPhysicsBody()->setDynamic(false);
			return true;   //如果点击的是精灵 则获取当前精灵 否则创建新的精灵
		}
	}
	addNewSpriteAtPosition(location);
	return true;
}

void HelloWorld::onTouchMoved(Touch* touch, Event* event)
{
	if (touchedS != nullptr)
	{
		touchedS->setPosition(touch->getLocation());
	}
	
}

void HelloWorld::onTouchEnded(Touch * touch,Event * event)
{
	if (touchedS != nullptr)
	{
		//touchedS->getPhysicsBody()->setDynamic(true);
		touchedS = nullptr;
	}
	
}

void HelloWorld::addNewSpriteAtPosition(Vec2 p)
{
	auto sp = Sprite::create("HelloWorld.png");
		sp->setTag(1);
		auto body = PhysicsBody::createCircle(sp->getContentSize().width / 2);
		sp->setPhysicsBody(body);
		sp->setPosition(p);
		sp->setScale(0.05);
		
		this->addChild(sp);

		SP.pushBack(sp);
}

174932_fjwq_2276921.png

转载于:https://my.oschina.net/u/2276921/blog/603644

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值