Cocos2dx遊戲教程(四):“見縫插針”,第一個場景的建立

從這一篇開始就正是進入遊戲具體實現啦,前面都瞭解了核心類的基本操作,那麼現在我們開始具體實現吧

一、見縫插針的玩法

簡單的說,玩家有一些小球,將這些小球彈射到大球上且不能碰到大球上的小球哦,前面介紹的“口紅機”就是這樣的玩法,只不過我們把小球換成了口紅而已。

看看廣告詞,說的還是很清楚的麼
又是一款簡單而不簡略的遊戲,遊戲畫面簡略但玩法新穎。玩家手中有一定數量的針頭,必須把所有的針頭都插進旋轉的球裏頭。不能夠重疊也不能夠撞到球上的針。趕緊來挑戰下你能闖到第幾關呢

我們知道了遊戲玩法,那麼我們開始設計自己的遊戲吧!

二、建立第一個場景

前面大家已經知道了一些名詞,場景,層,精靈等,下面帶領大家建立第一個場景,遊戲開屏界面,按版署要求,第一頁需要來一個《健康遊戲公告》的聲明,如下所示,那麼我們如何實現這個界面呢?
在這裏插入圖片描述
1、創建WelcomeScene類

我們仿照HelloworldScene創建一個WelcomeScene

頭文件如下WelcomeScene.h

#pragma once

#include "cocos2d.h"

class WelcomeScene : public cocos2d::Layer
{
public:
	static cocos2d::Scene* createScene();

	virtual bool init();

	// a selector callback
	void menuCloseCallback(cocos2d::Ref* pSender);

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

類文件如下WelcomeScene.cpp

#include "WelcomeScene.h"

USING_NS_CC;

Scene* WelcomeScene::createScene()
{
	// 'scene' is an autorelease object
	auto scene = Scene::create();

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

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

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

	// 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);

	return true;
}

我們首先製作一張公告的背景圖片,然後將其加載到layer上,是不是又回到了上篇介紹的加載方式啊,測試的時候別忘了將AppDelegate.cpp中的場景跳轉改成該場景啊,用到了初始化的director->runWithScene(scene);了吧

// create a scene. it's an autorelease object
auto scene = WelcomeScene::createScene();
// run
director->runWithScene(scene);

三、運行第一個場景

我們已經完成了第一個場景的創建,我們運行下看看是什麼樣子的呢

在這裏插入圖片描述

驚不驚喜,意不意外,是不是感覺沒有那麼複雜~
在這裏插入圖片描述
我們第一個場景運行了,那麼如何到下一個場景運行呢
我們的導演類Director將再次出場,跳轉場景開始啦,新場景在哪?當然在創建一個啊。第一個場景會建立了,第二個還會遠麼

Director::getInstance()->replaceScene(???);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章