Jump to content

Using Random Math To Choose An Image?


Recommended Posts

OK SO.  I have NO programming background.  No skills whatsoever.  This is like my first try, after messing around with HelloPhaser and doing the tutorial for Your First Phaser Game.  I think I have a good grasp of coding, but I can't get what I want to happen to work. :C


This will be a farming game.  When the game starts for the first time, I'd like for three plots of land to generate, using a number 1 - 10.  1 is poor soil, 2 - 8 is normal quality and 9 - 10 is good quality.  Here's what I have so far.  What needs changing to make this work?

function preload() {    game.load.image('startbg', 'assets/choosebg.png');    game.load.image('poor', 'assets/poorsoil.png');    game.load.image('norm', 'assets/normalsoil.png');    game.load.image('good', 'assets/goodsoil.png');var plot1;var plot2;var plot3;}function create() {    game.add.sprite(0, 0, 'startbg');        plot1 = [];    Math.floor((Math.random()*10)+1) {};        if (plot1 == 1);            {game.add.sprite(190, 250, 'poor');}        else if (plot1 == 2,3,4,5,6,7,8);            {game.add.sprite(190, 250, 'norm');}        else (plot1 == 9,10);            {game.add.sprite(190, 250, 'good');

P.S.  I'm sorry if this isn't the right section for this question. :C

Edited by rich
Formatted code
Link to comment
Share on other sites

Quite a lot of mistakes in such a few lines of code :)


But you can't do this:

if (plot1 === 2 || plot1 === 3 || plot1 === 4)

and so on. Or a much easier way to do it:

if (plot1 >= 2 && plot1 <= 8)

Also don't put a semi colon at the end of the "if" line - that terminates the line!


Without meaning to be rude I would suggest you do a few crash courses in JavaScript :) There are so many excellent resources out there these days, covering so many different learning styles.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...