Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Logan

I'm spawning a couple enemies on the start of a level. Inside the enemy.js I have a random number generator getting a value between 1-5 but instead of each enemy.js getting a random number when spawning they all seem to share the same value as the first entity spawned?

drop: Math.floor((Math.random()*5)+1),

1 decade ago by Joncom

Let's see some more code surrounding the spawning of the entities please.

1 decade ago by Logan

Hi joncom!

Thanks for your response but I just fixed my issue. I was calling the random number generator outside the init: function like the following.

/**
 * Created by Logan Looker on 10/31/13.
 */

ig.module(
        'game.entities.enemy'
    )
    .requires(
        'impact.entity',
        'game.entities.player',
        'game.entities.bullet',
        'game.entities.ammo',
        'game.entities.medkit',
        'game.entities.healthbar'
    )
    .defines(function(){

        EntityEnemy = ig.Entity.extend({

            font: new ig.Font( 'media/04b03.font.png' ),

            //Enemy Properties

            size: {x:32,y:48},
            drop: Math.floor((Math.random()*5)+1),        //Sets a random drop number for the enemies drop on death
            speed: Math.floor((Math.random()*50)+10),                   //Sets a random speed for the enemy

            //Collision Detection

            collides: ig.Entity.COLLIDES.PASSIVE,
            type: ig.Entity.TYPE.B,
            checkAgainst: ig.Entity.TYPE.A,

            //Animation Sheet

            animSheet: new ig.AnimationSheet( 'media/tilesets/enemy1.png', 32, 48),

            init: function( x, y, settings ) {
                this.parent( x, y, settings );

            },

I was getting the same random number throughout every enemy, it would generate a random number for the first enemy and then use that same number for all enemies instead of each enemy generating their own independent drop and speed..

After placing the random generating code into the init: function I got the results I was looking for.. Each enemy has its own independent random speed and drop. Like this:

/**
 * Created by Logan Looker on 10/31/13.
 */
ig.module(
        'game.entities.enemy'
    )
    .requires(
        'impact.entity',
        'game.entities.player',
        'game.entities.bullet',
        'game.entities.ammo',
        'game.entities.medkit',
        'game.entities.healthbar'
    )
    .defines(function(){

        EntityEnemy = ig.Entity.extend({

            font: new ig.Font( 'media/04b03.font.png' ),

            //Player Properties
            size: {x:32,y:48},
            drop: 0,
            speed: 0,

            //Collision Detection
            collides: ig.Entity.COLLIDES.PASSIVE,
            type: ig.Entity.TYPE.B,
            checkAgainst: ig.Entity.TYPE.A,

            //ANIMATION SHEET
            animSheet: new ig.AnimationSheet( 'media/tilesets/enemy1.png', 32, 48),

            init: function( x, y, settings ) {
                this.parent( x, y, settings );

                //RANDOM GENERATORS!
                this.drop = Math.floor((Math.random()*5)+1); //Sets a random drop number for the enemies drop on death
                this.speed = Math.floor((Math.random()*50)+10); //Sets a random speed for the enemy
                //RANDOM GENERATORS!


                if( !ig.global.wm )
                {
                    ig.game.spawnEntity(EntityHealthbar, this.pos.x, this.pos.y,{ Unit: this });
                }

            },

1 decade ago by dominic

Well, you figured it out already, but just to clarify:

var foo = function() {
    console.log('foo called');
    return Math.random();
};

var bar = function() {
    console.log('bar called');
    return Math.random();
};

MyClass = ig.Class.extend({
    // foo() is only called once, when the class is created
    onlyRandomForThisClass: foo(),

    init: function() {
        // bar() is called for each instance you create of this class
        this.randomForEachInstance = bar();
    }
});

console.log('class created');

var instance1 = new MyClass();
var instance2 = new MyClass();


This will produce the following log messages:
foo called
class created
bar called
bar called

This has also been a common pitfall with Impact&039;s #ig.Timer class. If you don&039;t want all class instances to share the same #ig.Timer instance, you have to create your timers in the init() method, not as class properties.

1 decade ago by Logan

This has also been a common pitfall with Impact's ig.Timer class. If you don't want all class instances to share the same ig.Timer instance, you have to create your timers in the init() method, not as class properties.


Thanks, good to know I'm gonna have to start using some timers here soon :D
Page 1 of 1
« first « previous next › last »