场景的总结以及音效(四)

cocos中场景总结

1. 场景是保存在Director类中,使用栈结构保存

2. 活动场景其实就是栈顶的场景
3. replaceScene就是用参数指定场景,替换栈顶的场景
4. pushScene实际是往栈顶增加一个场景,原来的栈顶场景被暂停
5. popScene删除栈顶场景,让下一个栈顶的场景成为活动场景



需要注意的问题

    popScene到栈为空的时候,程序退出
    pushScene时,由于旧场景依旧保存在内存,会导致内存使用增多,慎用pushScene

    场景切换可以用动画。。。。

1. 只有replaceScene时才能有动画,pushScene不能有

2. 场景切换动画需要持续一段时间,这一段时间会有内存峰值的出现

3.解决内存峰值的方法:

  /*  1.给老场景拍照
      2.清空老场景使用的内存
      3.把拍照好的照片作为一个精灵放在场景中间,
      4.创建新的场景
      5.执行场景切换*/
</pre><pre>

#ifndef __T011301CaptureScreen_H__
#define __T011301CaptureScreen_H__
#include "cocos2d.h"
USING_NS_CC;

class T011301CaptureScreen : public Layer
{
public:
	T011301CaptureScreen();
	~T011301CaptureScreen();

	CREATE_FUNC(T011301CaptureScreen);

	void AfterCapture(bool result, const std::string& str)
	{
		Sprite* sprite = Sprite::create(str.c_str());

		if (result)
		{
			CCLOG("cpature ok");
		}
	}

	bool touchBegan(Touch*, Event*)
	{
	//方法一
		utils::captureScreen(CC_CALLBACK_2(T011301CaptureScreen::AfterCapture, this), "d:\\test.png");
#if 0
     //方法二
		Size  winSize = Director::getInstance()->getWinSize();
		// 拍照
		RenderTexture* rt = RenderTexture::create(winSize.width, winSize.height);
		rt->begin();
		//Director::getInstance()->getRunningScene()->visit();
		this->visit();
		rt->end();

		addChild(rt);
		rt->setPosition(winSize.width / 2, winSize.height / 2+100);

#endif
		return true;
	}

	bool init()
	{
		Layer::init();

		Size  winSize = Director::getInstance()->getWinSize();

		Sprite* sprite = Sprite::create("Images/blocks.png");
		addChild(sprite);
		sprite->setPosition(winSize.width/2, winSize.height/2);

		EventListenerTouchOneByOne* ev = EventListenerTouchOneByOne::create();
		ev->onTouchBegan = CC_CALLBACK_2(T011301CaptureScreen::touchBegan, this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this);

		return true;
	}
};

#endif

音效

        音效是一个游戏的灵魂, 一个好游戏必须要有恰到好处的音乐衬托。以下是一个基本的使用方法示例。
</pre><pre>

#ifndef __T011401Audio_H__
#define __T011401Audio_H__

#include "cocos2d.h"
USING_NS_CC;

#include "SimpleAudioEngine.h"
using namespace CocosDenshion;

class T011401Audio : public Layer
{
public:
	T011401Audio();
	~T011401Audio();

	CREATE_FUNC(T011401Audio);

	bool init()
	{
		Layer::init();

#if 0
		SimpleAudioEngine* sae = SimpleAudioEngine::getInstance();

		// 播放背景音乐
		sae->playBackgroundMusic("audio/LuckyDay.mp3", true);
		// 停止背景音乐
		//sae->stopBackgroundMusic(true);

		// 播放游戏音效
		// 音效不止一个,所以音效有id
		int id = sae->playEffect("audio/SoundEffectsFX009/FX083.mp3");
		// 停止播放音效
		//sae->stopEffect(id);

		sae->setBackgroundMusicVolume(0.5f);
		sae->setEffectsVolume(0.5f);

		sae->pauseBackgroundMusic();
		sae->pauseAllEffects();

		sae->resumeBackgroundMusic();
		sae->resumeAllEffects();

#endif
		Size winSize = Director::getInstance()->getWinSize();
		Size visibleSize = Director::getInstance()->getVisibleSize();
		Point origin = Director::getInstance()->getVisibleOrigin();

		CCLOG("winSize.width=%f. winSize.height=%f", winSize.width, winSize.height);
		CCLOG("visibleSize.width=%f. visibleSize.height=%f", visibleSize.width, 
visibleSize.height); 
		CCLOG("origin.x=%f, origin.y=%f", origin.x, origin.y);



		return true;
	}
};

#endif







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值