Ninjadoodle Posted September 21, 2018 Share Posted September 21, 2018 Hi @enpu / Panda People So, I have some robot classes and I would like to push them to an array, so I can randomly pick and create one when needed. How would I go about doing this - I know what I need to do, just kinda stuck on how to implement it Thank you in advance for any tips! game.createClass('Robot1', { init: function() { this.id = 'robot1'; this.circle1 = new game.ShapeCircle( -240, -240, 80, '#61C2C2'); this.circle2 = new game.ShapeCircle(240, -240, 80, '#61C2C2'); this.square1 = new game.ShapeSquare(0, -240, 320, 320, 1.25, '#66CCCC'); this.square2 = new game.ShapeSquare(0, -400, 160, 160, 1.25, '#61C2C2'); } }); game.createClass('Robot2', { init: function() { this.id = 'robot2'; this.circle1 = new game.ShapeCircle( -240, -240, 160, '#61C2C2'); this.circle2 = new game.ShapeCircle(240, -240, 160, '#61C2C2'); this.square1 = new game.ShapeSquare(0, -240, 320, 320, 1.25, '#66CCCC'); this.square2 = new game.ShapeSquare(0, -400, 160, 160, 1.25, '#61C2C2'); } }); game.createClass('Robot3', { init: function() { this.id = 'robot3'; this.circle1 = new game.ShapeCircle( -240, -240, 240, '#61C2C2'); this.circle2 = new game.ShapeCircle(240, -240, 240, '#61C2C2'); this.square1 = new game.ShapeSquare(0, -240, 320, 320, 1.25, '#66CCCC'); this.square2 = new game.ShapeSquare(0, -400, 160, 160, 1.25, '#61C2C2'); } }); Quote Link to comment Share on other sites More sharing options...
enpu Posted September 21, 2018 Share Posted September 21, 2018 var robots = ['Robot1', 'Robot2', 'Robot3']; var robot = new game[robots.random()]; Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted September 21, 2018 Author Share Posted September 21, 2018 @enpu - Wow! That simple lol - thanks heaps!! Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted September 21, 2018 Author Share Posted September 21, 2018 @enpu - One thing - I get a warning with this line ... var robot = new game[robots.random()]; 'Missing '()' invoking a constructor' Anything to worry about? Quote Link to comment Share on other sites More sharing options...
enpu Posted September 21, 2018 Share Posted September 21, 2018 I did not get any warnings. Can you show how did you made robots var? Did you get that warning in the editor's JS console? Or in external browser? EDIT: Try this: var robot = new game[robots.random()](); Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted September 21, 2018 Author Share Posted September 21, 2018 @enpu- editors JS console - it works, i just get a warning game.createScene('Game', { score: 0, parts: 0, count: 0, robotArray: ['Robot1', 'Robot2', 'Robot3'], init: function() { // SETUP this.stageSetup = new game.StageSetup(); this.stageSetup.setupStage(); // TIMER this.timerBar = new game.TimerBar(60); this.timerBar.start(); // ROBOTS this.robotContainer = new game.Container().addTo(game.scene.mg); this.robotContainer.position.set(640, 1200); this.robot = new game[this.robotArray[this.count]]; } }); }); Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted September 21, 2018 Author Share Posted September 21, 2018 @enpu - Nice! That gets rid of the warning Thanks! 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.