首页 > 代码库 > cocos2d-x 3.0游戏实例学习笔记 《跑酷》一 开始界面

cocos2d-x 3.0游戏实例学习笔记 《跑酷》一 开始界面

开始界面和前辈的基本上差不多:cocos2d-x游戏开发 跑酷(一)开始界面     但是也有不同

下面先给出游戏过程中用到的所有资源

开始界面定义为:MainScene 有如下功能:

1.背景

2.进入游戏界面按钮

3.预先加载背景音乐文件

.h文件

#ifndef __MainScene__H__
#define __MainScene__H__

#include "cocos2d.h"

class MainScene : cocos2d::Layer{
public:
	virtual bool init();
	static cocos2d::Scene* scene();
	CREATE_FUNC(MainScene);

private:
	//
	void createBG();
	//
	void createButton(cocos2d::Point centerPoint);
	//
	void preLoadMusic();
	//menu start the game
	void start();
};/**/

#endif
.cpp
#include "MainScene.h"
#include "SimpleAudioEngine.h"
//#include "PlayScene.h"

USING_NS_CC;
using namespace CocosDenshion;

Scene* MainScene::scene(){
	Scene* scene = Scene::create();
	Layer* layer = MainScene::create();
	scene->addChild(layer);
	return scene;
}

bool MainScene::init(){
	if(!Layer::init()){
		return false;
	}

	//preloadMusic
	preLoadMusic();

	//create background
	createBG();

	return true;
}

void MainScene::createBG(){
	auto winSize = Director::getInstance()->getWinSize();
	auto centerPoint = ccp(winSize.width / 2, winSize.height / 2);

	//BG
	Sprite* spriteBg = Sprite::create("MainBG.png");
	spriteBg->setPosition(centerPoint);
	this->addChild(spriteBg);

	//create button
	createButton(centerPoint);
}

void MainScene::createButton(cocos2d::Point centerPoint){
	//Start mutton
	MenuItemImage* menuItem = MenuItemImage::create(
		"start_n.png",
		"start_s.png",
		CC_CALLBACK_0(MainScene::start,this));

	Menu* menu = Menu::create(menuItem,NULL);

	menu->setPosition(centerPoint);

	this->addChild(menu);
}

void MainScene::preLoadMusic(){
	SimpleAudioEngine* audioEngin = SimpleAudioEngine::getInstance();
	audioEngin->preloadBackgroundMusic("background.mp3");
	audioEngin->preloadEffect("jump.mp3");
	audioEngin->preloadEffect("crouch.mp3");
}

void MainScene::start(){
	CCLOG("game is start !");
	//Scene* playScene = TransitionFade::create(1,PlayScene::createScene());
	//Director::getInstance()->replaceScene(playScene);
}
这里由于我对屏幕自适应的知识还不够了解,虽然看过几篇文章,但是还是比较迷茫,不怎么知道运用到实例当中,所以对于这次重写中,背景图片一般都是 640*320大小的 ,所以需要在AppDelegate.cpp 当中设置显示窗口大小:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLView::create("My Game");
	glview->setFrameSize(640,320);//------修改显示窗口大小
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
	auto scene = MainScene::scene();

    // run
    director->runWithScene(scene);

    return true;
}
自己只能后面了解相关知识啦,如果有好的相关教程,麻烦推荐一下,谢谢!!

运行之后如图:


游戏资源:跑酷游戏资源

个人愚昧观点,欢迎指正与讨论大笑