redrory Posted October 28, 2014 Share Posted October 28, 2014 Good day guys, I'm customizing Flying Dog, I've gone throught the tutorials and documentation. Still a bit unclear on how to implement something. After the round ends, when I click restart. I want another plane to be used for the reminder of the game. I know I have to add it to assets.js, and then objects.js under this.sprite. However I'm not sure how to enable the new plane on a button action or restart. On restart button click - it's loads game.system.setScene(SceneGame); So I was trying to however load another scene, however I think that would be overkill, all I want is a different player/sprite for the same scene. Any assistance would be much appreicated. Cheers Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 You can set global variables to scene object, like this:game.SceneMain.player = 1;And then use that to change your player sprite, something like this:// Restart buttonrestartButton.click = function() { // Switch player variable between 0 and 1 if (game.SceneMain.player === 0) game.SceneMain.player++; else game.SceneMain.player = 0; game.system.setScene('Main');};// Player spritethis.sprite = new game.Sprite('player' + game.SceneMain.player + '.png');Did that make sense to you? Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 Hey, Thanks for your reply, I have a high level understanding.A few questons: When I try to set the global variable - SceneMain or SceneGamegame.SceneMain.player = 1;I get the following error - Cannot read property 'player' of undefined Also the player sprite line - is that handled during scenes or objects? Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 Your error means, that there is no game.SceneMain object found. Create your scene, before defining variables to it:game.createScene('Main', {});game.SceneMain.player = 1;And the player sprite line is for Player class (found inside objects.js). Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 I beleive that I already have created or using the default scene in the flying dog code (in scenes.js). Do I still have to create? Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 Make sure the scene name is correct, if you create scene named Game, then the object is game.SceneGame etc. Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 Hey, So I fixed that undefined erorr. I've added the Sprite code to objects.js, my issue is that it doesn't seems to be incrementing by the time it gets to the new or restarted scene. I've added the global variable game.scene.player = 1; to my objects.js right above where I add my sprites. My question is - in my restartButton, when I increments how does that ++ value reach back to my sprite to then show the new player. scenes.jsthis.restartButton = new game.Sprite('media/restart.png', game.system.width / 2, game.system.height / 2 + 225, { anchor: {x:0.5, y:0.5}, scale: {x:0, y:0}, interactive: true, mousedown: function() { game.analytics.event('restart'); console.log('Before number ' + game.scene.player); game.scene.player = 1; if (game.scene.player === 0) game.scene.player++; else game.scene.player = 0; game.system.setScene(SceneGame); console.log('Random number is ' + game.scene.player); } });objects game.scene.player = 1; this.sprite = new game.MovieClip([ game.Texture.fromImage('media/player' + game.scene.player + '.png'), game.Texture.fromImage('media/player' + game.scene.player + '.png'), ]); Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 Don't put your variable in game.scene, that is cleared every time you change scene. Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 Removed it, however still not incrementing the sprite number. To me, it seems that I should be passing the incremented value somewhere in setScenegame.system.setScene(SceneGame); Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 put your variable in game.SceneMain.player Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 I have to ask, while file would that be in? Quote Link to comment Share on other sites More sharing options...
enpu Posted October 28, 2014 Share Posted October 28, 2014 In your code above, replace game.scene.player with game.SceneMain.player Quote Link to comment Share on other sites More sharing options...
redrory Posted October 28, 2014 Author Share Posted October 28, 2014 When I do that, I get the following : Uncaught TypeError: Cannot read property 'player' of undefined As i assume I don't have scene by that name. My scene name seems to be game.scene - as I use the following to add buttons to it.game.scene.stage.addChild(box);I'm thinking do I have to create a SceneMain scene or can I use the one that I have below? Quote Link to comment Share on other sites More sharing options...
redrory Posted October 29, 2014 Author Share Posted October 29, 2014 Hey, So I've created another scene, pretty much by copying the default scene. So on my button click I'm now loading that different scene, which is half the problem however, I'm still unsure how to load a different sprite within that new scene. Mostly because objects aren't handled within the scene.js but within objects. I'm not even seeing, where I'm calling my sprite from scene, for me to me able to call a different one. Quote Link to comment Share on other sites More sharing options...
redrory Posted October 29, 2014 Author Share Posted October 29, 2014 Hey, Got it working, in a hackish way. Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.