逃离地心——齿轮旋转

效果图:



.h文件:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();
	// 设置世界
	void setPhyWorld(cocos2d::PhysicsWorld* world){ m_world = world; }
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
private:
	// 世界
	cocos2d::PhysicsWorld* m_world;
};

#endif // __HELLOWORLD_SCENE_H__

.cpp文件:

#include "HelloWorldScene.h"

USING_NS_CC;
Scene* HelloWorld::createScene()
{
	auto scene = Scene::createWithPhysics();
	// 设置调试模式
	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
	// 创建图层
	PhysicsWorld* phyWorld = scene->getPhysicsWorld();
	phyWorld->setGravity(Vec2(0, 0));
	auto layer = HelloWorld::create();
	// 为图层指定世界
	layer->setPhyWorld(scene->getPhysicsWorld());
	// 添加图层到场景
	scene->addChild(layer);
	// 返回场景
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
	/////////
	auto triangle01 = Sprite::create("02.png");
	triangle01->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	triangle01->setScale(0.18);
	
	auto polygonBody = PhysicsBody::create();
	Vec2 vec[3] = { 
		Vec2(-39.00000, 814.50000),
		Vec2(15.00000, 742.50000),
		Vec2(-291.00000, 668.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec, 3));
	Vec2 vec01[3] = {
		Vec2(-39.00000, 814.50000),
		Vec2(15.00000, 742.50000),
		Vec2(-291.00000, 668.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec01, 3));
	Vec2 vec02[3] = {
		Vec2(-723.50000, -165.00000),
		Vec2(-812.50000, -61.00000),
		Vec2(-658.00000, 420.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec02, 3));
	Vec2 vec03[4] = {
		Vec2(-723.50000, -165.00000),
		Vec2(-460.50000, -581.00000),
		Vec2(-559.00000, -592.50000),
		Vec2(-812.50000, -61.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec03, 4));
	Vec2 vec04[4] = {
		Vec2(5.00000, -741.50000),
		Vec2(86.00000, -809.50000),
		Vec2(-559.00000, -592.50000),
		Vec2(-460.50000, -581.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec04, 4));
	Vec2 vec05[4] = {
		Vec2(542.00000, -505.50000),
		Vec2(609.50000, -540.00000),
		Vec2(86.00000, -809.50000),
		Vec2(5.00000, -741.50000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec05, 4));
	Vec2 vec06[3] = {
		Vec2(739.50000, -54.00000),
		Vec2(609.50000, -540.00000),
		Vec2(542.00000, -505.50000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec06, 3));
	Vec2 vec07[5] = {
		Vec2(-39.00000, 814.50000),
		Vec2(-38.00000, 814.50000),
		Vec2(520.50000, 627.00000),
		Vec2(548.00000, 499.50000),
		Vec2(15.00000, 742.50000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec07, 5));
	Vec2 vec08[3] = {
		Vec2(739.50000, -54.00000),
		Vec2(806.50000, 112.00000),
		Vec2(609.50000, -540.00000),

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec08, 3));
	Vec2 vec09[4] = {
		Vec2(548.00000, 499.50000),
		Vec2(520.50000, 627.00000),
		Vec2(806.50000, 112.00000),
		Vec2(739.50000, -54.00000)

	};
	polygonBody->addShape(PhysicsShapePolygon::create(vec09, 4));
	triangle->setPhysicsBody(polygonBody);
	//ActionInterval* cc01 = (ActionInterval*)triangle->runAction(RotateBy::create(6, -360));
	//triangle->runAction(RepeatForever::create(cc01));
	addChild(triangle);

	///////////////////
	auto polygonBody01 = PhysicsBody::create();
	Vec2 veca[3] = {
		Vec2(-39.00000, 814.50000),
		Vec2(15.00000, 742.50000),
		Vec2(-291.00000, 668.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(veca, 3));
	Vec2 vec01a[3] = {
		Vec2(-39.00000, 814.50000),
		Vec2(15.00000, 742.50000),
		Vec2(-291.00000, 668.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec01a, 3));
	Vec2 vec02a[3] = {
		Vec2(-723.50000, -165.00000),
		Vec2(-812.50000, -61.00000),
		Vec2(-658.00000, 420.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec02a, 3));
	Vec2 vec03a[4] = {
		Vec2(-723.50000, -165.00000),
		Vec2(-460.50000, -581.00000),
		Vec2(-559.00000, -592.50000),
		Vec2(-812.50000, -61.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec03a, 4));
	Vec2 vec04a[4] = {
		Vec2(5.00000, -741.50000),
		Vec2(86.00000, -809.50000),
		Vec2(-559.00000, -592.50000),
		Vec2(-460.50000, -581.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec04a, 4));
	Vec2 vec05a[4] = {
		Vec2(542.00000, -505.50000),
		Vec2(609.50000, -540.00000),
		Vec2(86.00000, -809.50000),
		Vec2(5.00000, -741.50000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec05a, 4));
	Vec2 vec06a[3] = {
		Vec2(739.50000, -54.00000),
		Vec2(609.50000, -540.00000),
		Vec2(542.00000, -505.50000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec06a, 3));
	Vec2 vec07a[5] = {
		Vec2(-39.00000, 814.50000),
		Vec2(-38.00000, 814.50000),
		Vec2(520.50000, 627.00000),
		Vec2(548.00000, 499.50000),
		Vec2(15.00000, 742.50000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec07a, 5));
	Vec2 vec08a[3] = {
		Vec2(739.50000, -54.00000),
		Vec2(806.50000, 112.00000),
		Vec2(609.50000, -540.00000),

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec08a, 3));
	Vec2 vec09a[4] = {
		Vec2(548.00000, 499.50000),
		Vec2(520.50000, 627.00000),
		Vec2(806.50000, 112.00000),
		Vec2(739.50000, -54.00000)

	};
	polygonBody01->addShape(PhysicsShapePolygon::create(vec09a, 4));
	triangle01->setPhysicsBody(polygonBody01);
	ActionInterval* cc02 = (ActionInterval*)triangle01->runAction(RotateBy::create(1.5, -360));
	triangle01->runAction(RepeatForever::create(cc02));
	addChild(triangle01);
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


posted @ 2016-06-14 21:35  Anzhongliu  阅读(192)  评论(0编辑  收藏  举报