java转cocos2d-x:场景切换

环境配置:刚开始,用vs2012去写代码,太不习惯了,特别是快捷键,还是转到ecplise上来,毕竟每天用ecplise写代码,说下把代码导入ecplise需要注意以下几点:

1.安装ndk,配置环境变量

2.导入libcocos2b至ecplise,项目要引用

3.设置c/c++ build

4.注意Android.mk文件

每次ctrl+s都会自动编译,出现

[armeabi] SharedLibrary  : libcocos2dcpp.so
[armeabi] Install        : libcocos2dcpp.so => libs/armeabi/libcocos2dcpp.so就代表so库生成了,然后运行程序,就可以跑起来了。

场景切换:cocos2d 主要是有director,scene,layer,ui这些组成,个人理解,director类似于android上的activity,scene类似于fragment,layer类似于layout,然后ui都是差不多,director可以切换场景,场景里面可以添加layer,layer里面可以添加ui控件(button,imageview,label,menu等,还可以添加精灵(这里还没去详细学习))。那么这么说来,我们就可以像android activity切换fragment一样的思路去实现它咯,我们先添加两个场景,

场景1: HelloWorld2.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"
#include "ui/uiwidget.h"
using namespace cocos2d::ui;
USING_NS_CC; 
class HelloWorld2 : public cocos2d::Layer
{
public:
<span style="white-space:pre">	</span>//create scene
<span style="white-space:pre">	</span>static cocos2d::Scene* createScene();
<span style="white-space:pre">	</span>// init layer
<span style="white-space:pre">	</span>virtual bool init();
<span style="white-space:pre">	</span>//menu touch callback
<span style="white-space:pre">	</span>void menuCloseCallback(cocos2d::Ref* pSender);
<span style="white-space:pre">	</span>//button touch callback
<span style="white-space:pre">	</span>void touchBack(cocos2d::Ref* pSender, TouchEventType type);
<span style="white-space:pre">	</span>CREATE_FUNC(HelloWorld2);
};


#endif // __HELLOWORLD_SCENE_H__

这里在头文件里面定义了 创建scene函数,初始化layer,两个回调函数,还有一个宏定义是负责初始化的

看下cpp

#include "HelloWorldScene.h"
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include  "MyScene.h"
using namespace cocos2d::ui;
USING_NS_CC;
Scene* HelloWorld2::createScene() {
	// 'scene' is an autorelease object
	auto scene = Scene::create();

	// 'layer' is an autorelease object
	auto layer = HelloWorld2::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 HelloWorld2::init() {
	//
	// 1. super init first
	if (!Layer::init()) {
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	/

	auto myButton = MenuItemImage::create("p.png", "CloseSelected.png",
			CC_CALLBACK_1(HelloWorld2::menuCloseCallback, this));
	myButton->setPosition(
			Vec2(
					origin.x + visibleSize.width
							- myButton->getContentSize().width / 2,
					origin.y + myButton->getContentSize().height / 2));

	// create menu, it's an autorelease object
	auto menu2 = Menu::create(myButton, NULL);
	menu2->setPosition(Vec2(0.5, 0.5));
	this->addChild(menu2, 1);

	/
	// 3. add your codes below...

	// add a label shows "Hello World"
	// create and initialize a label

	auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf",
			24);

	// position the label on the center of the screen
	label->setPosition(
			Vec2(origin.x + visibleSize.width / 2,
					origin.y + visibleSize.height
							- label->getContentSize().height));

	// add the label as a child to this layer
	this->addChild(label, 1);

	// add "HelloWorld" splash screen"
	auto sprite = Sprite::create("HelloWorld.png");

	// position the sprite on the center of the screen
	sprite->setPosition(
			Vec2(visibleSize.width / 2 + origin.x,
					visibleSize.height / 2 + origin.y));

	// add the sprite as a child to this layer
	this->addChild(sprite, 0);

	//add label
	auto myLabel = Label::createWithTTF("aaaaaaaaaaaa", "fonts/Marker Felt.ttf",
			24);
	myLabel->setPosition(ccp(200.0, 200.0));
	this->addChild(myLabel, 1);
	//add button
	auto button = Button::create("p.png");
	button->setTitleText("button");
	button->setPosition(
			Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f));

	this->addChild(button, 1);
	//add Touch Event
	button->addTouchEventListener(this,
			toucheventselector(HelloWorld2::touchBack));

	// Create the imageview
	ImageView* imageView = ImageView::create("p.png");
	imageView->setPosition(
			Vec2(visibleSize.width / 3.0f, visibleSize.height / 3.0f));
	addChild(imageView, 1);

	// Create the imageview
	ImageView* imageView2 = ImageView::create("p.png");
	imageView2->setPosition(
			Vec2(visibleSize.width / 4.0f, visibleSize.height / 4.0f));
	addChild(imageView2, 1);

	return true;
}
//implements touch callback
void HelloWorld2::touchBack(Ref* sender, TouchEventType type) {
	switch (type) {
	case TOUCH_EVENT_ENDED:
		auto myScene = MyScene::createScene();
		Director::getInstance()->replaceScene(
				TransitionShrinkGrow::create(0.5f, myScene));
		break;

	}

}
void HelloWorld2::menuCloseCallback(Ref* pSender) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
	return;
#endif

	Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}
在这里就实现scene的创建,初始化layer,初始化控件,控件添加事件,实现回调函数,我们给buton添加了addTouchEventListener,从而就实现了button点击事件,然后在touchBack里面做场景切换。场景切换有两种:替换和栈式,替换主要是销毁了当前的scene,显示要替换的场景,栈式是保存当前scene,不销毁,显示后一场景,当后一场景销毁了会显示前一栈,这跟activity 跳转是一样的。

auto myScene = MyScene::createScene();
		Director::getInstance()->replaceScene(
				TransitionShrinkGrow::create(0.5f, myScene));
这里就用的就是替换的方式,director做替换,同时可以加上切换动画,下面是Myscene这个场景,以下只贴代码

MyScene.h

/*
 * MyScene.h
 *
 *  Created on: 2015-5-7
 *      Author: jj
 */

#ifndef MYSCENE_H_
#define MYSCENE_H_
#include "cocos2d.h"
#include "ui/UIWidget.h"
using namespace cocos2d::ui;
USING_NS_CC;
class MyScene:Layer {
public:
	MyScene();
	virtual ~MyScene();
	static Scene* createScene();
	virtual bool init();
	//touch event
	void touchCallBack(cocos2d::Ref* pSender,TouchEventType type);
	CREATE_FUNC(MyScene);
};
#endif /* MYSCENE_H_ */

MyScrene.cpp

/*
 * MyScene.cpp
 *
 *  Created on: 2015-5-7
 *      Author: jj
 */

#include "MyScene.h"
#include "HelloWorldScene.h"
#include "ui/CocosGUI.h"
using namespace cocos2d::ui;
USING_NS_CC;
MyScene::MyScene() {
	// TODO Auto-generated constructor stub
}

MyScene::~MyScene() {
	// TODO Auto-generated destructor stub
}
Scene* MyScene::createScene() {
	auto scene = Scene::create();
	auto layer = MyScene::create();
	scene->addChild(layer);
	return scene;
}
bool MyScene::init() {
	if (!Layer::init()) {
		return false;
	}

	Size visibleSize = Director::getInstance()->getVisibleSize();

	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf",
			24);
	// position the label on the center of the screen
	label->setPosition(
			Vec2(origin.x + visibleSize.width / 2,
					origin.y + visibleSize.height
							- label->getContentSize().height));
	// add the label as a child to this layer
	this->addChild(label, 1);

	auto button = Button::create("p.png");
	button->setTitleText("button");
	button->setPosition(
			Vec2(visibleSize.width / 2.0f, visibleSize.height / 2.0f));

	this->addChild(button, 1);
	button->addTouchEventListener(this,
			toucheventselector(MyScene::touchCallBack));

	return true;
}
void MyScene::touchCallBack(Ref* pSender, TouchEventType type) {
	switch (type) {
	case TOUCH_EVENT_ENDED:
		Director::getInstance()->replaceScene(
				TransitionShrinkGrow::create(0.5f, HelloWorld2::createScene()));
		break;

	default:
		break;
	}

}


到这里就实现了scene切换了,跟fragment切换太像了,哈哈,写android代码去咯,有时间继续学习。










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值