cocos2d-x background music's volume cannot change - cocos2d-x

When I use cocos2d-x to write a program, I found I could not change the volume of the music. Neither in the demo. So I open the source file. I found that the function setBackgroundMusicVolume is empty, and the function getBackgroundMusicVolume just return 1.0f.
I want to know why. The code is showed below:
float SimpleAudioEngine::getBackgroundMusicVolume()
{
return 1.0;
}
void SimpleAudioEngine::setBackgroundMusicVolume(float volume)
{
}
float SimpleAudioEngine::getEffectsVolume()
{
return 1.0;
}
void SimpleAudioEngine::setEffectsVolume(float volume)
{
}

Set Background Music Volume Effect
CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(1.0);

What platform you are looking at?
Different platform will have their own implementation; You can see those implementation in "CocosDenshion" folder accordingly.

Related

LibGDX Particles not the same as in Editor

I've created a "snow" particle effect using LibGDX's ParticleEditor, and exported the effect to a snow.p file.
When I load the file in my game it looks completely different.
As seen in the editor:
As seen in game:
As you can see the editor shows the snow falling throughout the space. Can't tell from the image, but it's falling downwards.
In the game, the snow "falls" along a line and to the right. I opened the p file in a text editor, and it looks fine so I'm not sure what is wrong.
In case it's needed, here is how I load and use the particle effect.
ParticleEffect p = new ParticleEffect();
p.load(Gdx.files.internal("snow.p"), Gdx.files.internal(""));
p.setPosition(0, DonutsGame.WORLD_HEIGHT / 2);
addActor(new ParticleEffectActor(p));
The actor class:
public class ParticleEffectActor extends Actor {
ParticleEffect effect;
public ParticleEffectActor(ParticleEffect effect) {
this.effect = effect;
effect.start();
}
public void draw(SpriteBatch batch, float parentAlpha) {
effect.draw(batch); //define behavior when stage calls Actor.draw()
}
public void act(float delta) {
super.act(delta);
effect.update(delta); //update it
}
}
I can post the snow.p file, but it's a bit large, if there's a particular section that would be helpful let me know and I will post it.

AS3 How to reload a game?

This code is an attempt to reset the stage to the original setup and start the game over, however it would only delete everything of the stage and leave it blank, it wont load the items on the stage. what am I missing here?
playLvlAgn.addEventListener(MouseEvent.CLICK, doLevelAgain);
function doLevelAgain(event: MouseEvent): void {
stage.removeChildren();
gotoAndPlay(1);
}
Just because things visually go away, it doesn't mean the objects in memory are magically purged.
You would need to handle this resetting yourself. As others have pointed out, to do this, it highly depends on your game's code and how it is organised. We simply can't provide any meaningful help as the possibilities for a game can be near infinite in the ways it is written.
As a somewhat related side thing:
If you are going to stick with frames (which others may be against, but I personally see them as useful in a variety of situations), I'd advise you to not start everything on frame 1. Again, I don't know how you've written your game, but it might be helpful to separate out certain functionality on certain frames.
Just an example:
Maybe asset loading can be on one frame (let's say frame 1)
Then on the next frame perhaps global persistent ui is created, such as help/settings menus. (frame 2)
Then the game level itself such as maps and layout + stuff that survives a reset can be built on the next (frame 3)
Then perhaps game play specific data + stuff that does not survive a reset, say player score and game time (frame 4)
Then the game starts (frame 5)
Then for a reset, if you want to just reset that level, you can do all your resetting/clean up logic, then jump to frame 4 and avoid reloading assets, recreating persistent ui + the physical game level.
It can get quite hairy if you weren't planning for this and in some cases, you may need to rethink/reorganise/rewrite some of your code. This is not a bad thing.
OK I found the solution. all removeChilds had to be inside the button function. I also had to sent it to frame2 and stop just in case someone attempted to reload the game before it was even started to play.
playLvlAgn.addEventListener(MouseEvent.CLICK, doLevelAgain);
function doLevelAgain(event: MouseEvent): void {
gotoAndStop(2);
for (var i: int = 0; i < 3; i++) {
if (balls.length > 0) {
ball = balls.pop();
ball.parent.removeChild(ball);
trace("if--0c")
}
}
if (stage.contains(playLvlAgn)) {
removeChild(playLvlAgn);
trace("if--1")
}
if (stage.contains(yesh8DoorA)) {
removeChild(yesh8DoorA);
trace("if--2")
}
if (stage.contains(yesh8DoorB)) {
removeChild(yesh8DoorB);
trace("if--3")
}
if (stage.contains(yesh8DoorC)) {
removeChild(yesh8DoorC);
trace("if--4")
}
if (stage.contains(yesh8allBuckets1)) {
removeChild(yesh8allBuckets1);
trace("if--5")
}
if (stage.contains(yesh8allBuckets2)) {
removeChild(yesh8allBuckets2);
trace("if--6")
}
if (stage.contains(add8bigGrayBall6)) {
removeChild(add8bigGrayBall6);
trace("if--7")
}
if (stage.contains(yesh8wall5)) {
removeChild(yesh8wall5);
trace("if--8")
}
if (stage.contains(yesh8step)) {
removeChild(yesh8step);
trace("if--9")
}
if (stage.contains(yesh8bomb26)) {
removeChild(yesh8bomb26);
trace("if--10")
}
if (stage.contains(yesh8bomb27)) {
removeChild(yesh8bomb27);
trace("if--10")
}
if (stage.contains(yesh8bomb28)) {
removeChild(yesh8bomb28);
trace("if--10")
}
if (stage.contains(yesh8bomb2)) {
removeChild(yesh8bomb2);
trace("if--11")
}
if (stage.contains(yesh8bomb3)) {
removeChild(yesh8bomb3);
trace("if--12")
}
if (stage.contains(yesh8gBall21)) {
removeChild(yesh8gBall21);
trace("if--13")
}
if (stage.contains(yesh8gBall24)) {
removeChild(yesh8gBall24);
trace("if--14")
}
if (stage.contains(yesh8redHalfSwing)) {
removeChild(yesh8redHalfSwing);
trace("if--15")
}
gotoAndPlay(1);
}

AS3: Call function does not work in browser

Here I have AS3 to for uploading recorded sound file to server. When I test it in Flash it works properly (record sound and upload it and goes to next frame) , but in browser it doesn't work. It seems can't call myUpload but I don't why? Is it should be mouse event? Thanks.
function VOCWordToYourMp3()
{
setTimeout(startRecording,3000);
recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
recorder.addEventListener(Event.COMPLETE, onRecordComplete);
}
function startRecording()
{
if (! recording)
{
recorder.record();
}
else if (recording)
{
recorder.stop();
recording = false;
}
}
function onRecording(e:RecordingEvent)
{
//
}
function onRecordComplete(e:Event):void
{
//
}
function renderWav(src, convertToMp3 = false)
{
//
function handleRenderTimer(e:TimerEvent)
{
//
}
function finishRender()
{
//
}
}
function makeIntoMp3(wav)
{
wav.position = 0;
mp3Encoder = new ShineMP3Encoder(wav);
mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
mp3Encoder.addEventListener(ProgressEvent.PROGRESS, mp3EncodeProgress);
mp3Encoder.start();
function mp3EncodeProgress(e:ProgressEvent):void
{
//
}
function mp3EncodeComplete(e: Event):void
{
myUpload('sound1',mp3Encoder.mp3Data);
}
}
function myUpload(namefile:String,sba: ByteArray):void
{
//upload code
}
Update:
In Flash Player 10 and Actionscript 3.0, all the calls to URLLoader must be in the same callstack.
http://helpx.adobe.com/flash-player/kb/user-interaction-required-upload-download.html
What is same callstack mean?
I recommend using something like Vizzy: https://code.google.com/p/flash-tracer/ to debug your swf in the browser. You can put trace statements into the onRecordComplete() and myUpload() functions, etc, to see how far the code is getting, and to see if you are getting any new errors. You may have some kind of security sandbox error because you are running in the browser. Being able to see what that error is will help you figure out what to do next.
To use Vizzy, you need to have the debug player running in your browser, and to configure the right path to your log file. This is sometimes a little bit tricky, so I'll give you some tips:
Add a file to your home directory called mm.cfg, and populate it with these settings:
ErrorReportingEnable=1
AS3Verbose=0
TraceOutputFileEnable=1
AS3Trace=0
TraceOutputBuffered=0
AS3StaticProfile=0
Then in Vizzy you have to set up the path to the log. On my windows 7 machine it is here:
C:\Users\MyUserName\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt
It may be different depending on your operating system.
Good luck, and report back if you Vizzy gives you any new information.

code for optimized watch

i want to implement a code to keep a watch on suppose some event ...at the meantime i don have any inbuilt eventwatcher so i hv to implement one of mine..which consumes least cpu & memory.
can u suggest me one..
for example a pseudocode is given:
while(true)
{
if(process.isrunning)
process.kill();
}
If you don't have any event to hook into, then your code has to be "active" to run the checks. And that costs CPU cycles.
What you can to do ease waste is to add a call to sleep (Thread.Sleep in .NET, sleep in some implementations of C++).
while (true) {
if(process.isrunning)
process.kill();
sleep(100); // Wait 100 millisecond before trying again
}
But that will make you code a little less responsive.
you can try using timer queue : http://msdn.microsoft.com/en-us/library/ms687003%28VS.85%29.aspx its basically using kernel scheduler to call your function callback at specified interval, the caller is from different thread so it won't interrupt the main thread and make your application responsive, the thread is managed by Windows so you don't have to manage your own pooling thread, and its relative accurate.
implementation example:
`
//a singleton class that hold timer queue
class TimerQueue {
protected:
HANDLE timerQueue;
TimerQueue() {
this->timerQueue = ::CreateTimerQueue();
}
~TimerQueue() {
if(this->timerQueue) {
::DeleteTimerQueueEx(this->timerQueue,NULL);
this->timerQueue = NULL;
}
}
public:
static HANDLE getHandle() {
static TimerQueue timerQueueSingleton;
return timerQueueSingleton.timerQueue;
}
}
//timer base class
class Timer
{
protected:
HANDLE timer;
virtual void timerProc() = 0;
static void CALLBACK timerCallback(PVOID param,BOOLEAN timerOrWait) {
Timer* self = (Timer*)param;
self->timerProc();
}
public:
Timer(DWORD startTimeMs,DWORD periodTimeMs) {
if(!::CreateTimerQueueTimer( &this->timer, TimerQueue::getHandle(),
(WAITORTIMERCALLBACK)&this->timerCallback,
this, startTimeMs, periodTimeMs,
WT_EXECUTEDEFAULT) ) {
this->timer = NULL;
}
}
virtual ~Timer() {
if(this->timer) {
::DeleteTimerQueueTimer(TimerQueue::getHandle(),&this->timer,NULL);
this->timer = NULL;
}
}
}
//derive and implement timerProc
class MyTimer : public Timer
{
protected:
virtual void timerProc() {
if(process.isRunning()) {
process.kill();
}
}
public:
MyTimer(DWORD startTimeMs,DWORD periodTimeMs)
: Timer(startTimeMs,periodTimeMs) {}
}
//usage:
int main(int argc,char* argv[]) {
MyTimer timer(0,100); //start immediately, at 10 Hz interval
}
`
disclaimer : i don't test or compile those codes, you should recheck it
Although you've tagged this as language-agnostic, any good implementation is going to vary widely not just from one language to another, but across operating systems. There are plenty of circumstances where programs or operating system functions need to do just this sort of thing, and mechanisms will have been implemented to do this in as sensible, non-intrusive a way as possible.
If you have a particular language and/or operating system in mind, please tell us, and give us a better idea of what you're trying to achieve. That way we can point you towards the most appropriate of the many possible solutions to this problem.

Is it okay to put game logic in a draw function?

I am making a game, and I have finally finished the gameplay aspect of it, but now it's time for me to create a menu and a high scores screen. I'm not entirely sure how to do it, the game will be in a different state (MENU_STATE, GAMEPLAY_STATE, SCORESCREEN_STATE) and in each state I want to draw different things to the screen, is it okay for me to do something like this then?
draw function()
{
if MENU_STATE
draw menu
if GAMEPLAY_STATE
draw game
if SCORESCREEN_STATE
draw scores
}
I've been following a strictly no logic in the draw function and it's been good so far, but I really can't figure out a different way to do this.
You could use separate classes for the three states, implementing a common interface, and rather than setting a constant for state, set an instance of one of the classes:
interface IState {
void draw();
}
class Menu implements IState {
void draw() {
// Draw menu
}
}
class Game implements IState {
void draw() {
// Draw game
}
}
void draw() {
state.draw();
}
This still isn't ideal (you don't really want drawing code in your state, you want something a bit more separate), but the abstraction is a common one and could be relevant (and it's hard to advise further without knowing more of your architecture).
You are calling some drawing functions in that routine but that doesn't mean
you have to name it draw.
Perhaps this is more appropriate in your case:
// pseudocode
on_game_state function(state)
{
select (state):
MENU_STATE:
draw menu
GAMEPLAY_STATE:
draw game
SCORESCREEN_STATE:
draw scores
}
Using a statemachine would make this simpler. Each state will have its own set of update and draw functions that are called when it is on top of the state stack. Instead of having one draw function with internal state switches you would have Game_Draw(), Menu_Draw(), HighScoreScreen_Draw() etc. Similarly your update functions could be separated out.
static void StateMachine_DrawTopState()
{
switch(stateMachine_topState)
{
case STATE_GAMEPLAY:
{
Gameplay_Draw();
}
break;
case STATE_MENU:
{
Menu_Draw();
}
break;
}
}
Similar to Andrew Aylett's answer and assuming an object-oriented language, perhaps you could do something like:
Interface IState {
void init();
void update();
void draw();
}
class GameplayScene implements IState {
void init() {
// initialize gameplay
}
void update() {
// update game logic
}
void draw() {
// draw game
}
}
class MenuScene implements IState {
void init() {
// initialize menu
}
void update() {
// update menu logic
}
void draw() {
// draw menu
}
}
class ScoresScene etc...
class TitleScene etc...
// Somewhere else, probably in the Game class
void Main() {
// Init game
Scene currentScene = new TitleScene;
while (Scene != null) {
Scene.init();
Scene.update();
Scene.draw();
}
// Exit game
}
You would also need to think about how to handle transition between scenes. You could have each scene class have a member variable called something like nextScene and the main function queries it at the start of the loop to switch to the proper scene.
If you don't have the luxury of using an object-oriented programming language (like C++, Java, C#, Python, etc.), both Colin's and Nick D's answers might help, although I'd try to have the switch statement in one place (say one big game_update function) to allow adding new states by making a change in one place. Alternatively, you could build on the Colin's state machine design to make something more generic and that doesn't explicitly require a hard-coded switch statement. (although to be honest I can't think of a good way to do it at the moment)
It is absolutely not ok to put game logic in a draw function.
However, if it makes your life easier in this specific case, it's ok anyway.
You can always change it later if it becomes a mess.
Yes it's fine, game programmers are allowed to bend the rules for performance gains. The view and the model of a game world are quite often one and the same thing to avoid latency created by decoupling the view and the model.
There's no reason why you can't make the menu and highscores objects part of your game world, it's been done before in quite a few games.