Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Got problems with random movement


DudeshootMankill
 Share

Recommended Posts

Hey folks i've got this problem here: 

 

When i use this code to spawn 10 members of the group "cell", only one of the spawned cells actually move.

 

function create() {
    
    
    cell = game.add.group();
    
    for (var i = 0; i < 10; i++)
    {
    var cell = this.game.add.sprite(game.world.randomX, game.world.randomY, 'cell');
    cell.game.time.events.loop(3500, function() {
      this.game.add.tween(cell).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);
    }, this)
    game.physics.enable(cell, Phaser.Physics.ARCADE);
    }
 }
Link to comment
Share on other sites

Welp, i fixed this problem myself by doing this:

 

    cell = game.add.group();
 
    for (var i = 0; i < 20; i++)
    {
    var cell2 = cell.create(game.world.randomX, game.world.randomY, 'cell');
    cell.game.time.events.loop(3500, function() {
      this.game.add.tween(cell).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);
    }, this)
    game.physics.enable(cell, Phaser.Physics.ARCADE);
    }
 
The problem now is that all the cells move in synchroninity, that was really not my idea. Does anyone know a clever way to make a group move randomly? I need the movement to be a slow loose movement.
Link to comment
Share on other sites

Use a random time to start movement.

sprite.time = Date.now();function timer(){                var current = Date.now(),            start = 1000,            end = 3000,            timing = game.rnd.integerInRange(start, end);                if(this.time + (current - this.time) > this.time + timing){            this.time = current;            return true;        }                return false;    }function update(){     if(timer.call(sprite)){         //move sprite     }}

When timer returns false the sprite waits to move. You can loop many sprites instead, and it will still work.

Link to comment
Share on other sites

sprite.time = Date.now();
function timer(){

var current = Date.now(),
start = 1000,
end = 3000,
timing = game.rnd.integerInRange(start, end);

if(this.time + (current - this.time) > this.time + timing){
this.time = current;
return true;
}

return false;
}

function update(){
if(timer.call(sprite)){
//move sprite
}
}

 

I just cant get this to work, can anyone give me the rest of the example i'd be very greatful, and perhaps a short explanaition of .time?

 

Sorry for being a dunce and all.

Link to comment
Share on other sites

.time is your own property. You can add what ever properties you want as long as they don't conflict with properties already there.

 

You could name it .date, .yippee, or what ever.

 

Sorry about the bad explanation. Try defining .time inside the create function.

function timer(){                var current = Date.now(),            start = 1000,            end = 3000,            timing = game.rnd.integerInRange(start, end);                if(this.time + (current - this.time) > this.time + timing){            this.time = current;            return true;        }                return false;    }function create(){  //create your sprite here  sprite.time = Date.now();}function update(){     if(timer.call(sprite)){         //move sprite     }}

You are not a dunce. :) I am.

Link to comment
Share on other sites

I can't get your example code to work at all and I'm to lazy to code it all myself.

game.time.events.loop(3500, function() {      this.game.add.tween(cell).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);    }, this);

I'm not a Phaser expert. Is it correct that this moves everything, every 3.5 seconds?

 

  • Do you maybe want to use cell2 instead of cell? (Better call "cell2" -> "newCell" and "cell" -> "cellGroup" or "cells" to avoid confusion.)
  • You probably also don't want to add the same event loop and set physics to arcade 20 times in a row. It has the same effect as doing it once.
  • If you don't use cell2, you can omit the "var cell2 =".

 

I would suggest to try this:

var nextMovedCell;var cell;function create() {    cell = game.add.group();    nextMovedCell = 0;    var interval = 3500/20;     for (var i = 0; i < 20; i++) {        cell.create(game.world.randomX, game.world.randomY, 'cell');    }    game.time.events.loop(interval, function() {        this.game.add.tween(cell.getAt(nextMovedCell)).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);                if (nextMovedCell < 20) {            nextMovedCell = nextMovedCell + 1;        } else {            nextMovedCell = 0;        }    }, this);    game.physics.enable(cell, Phaser.Physics.ARCADE);}

<group>.getAt(<index>) gets a single element of a group. What this is supposed to do is move an element of the group every 3500/20=175 milliseconds, but each time a different one.

 

I'm sorry if that doesn't help you. As I said, I'm not a Phaser expert and I don't know how any of the classes work. I could program this in another programming language.

 

You might want to post your whole code.

Link to comment
Share on other sites

I'm not a Phaser expert. Is it correct that this moves everything, every 3.5 seconds?

 

It will do something every 3.5 seconds.

 

I'm not a phaser expert either. I look at the docs, and the source code of phaser when I have to. No need to be an expert.

 

You'll figure it out if you're patient.

 

In your code cell is a group. You can't tween individual sprites through a group.

 

<group>.getAt(<index>) gets a single element of a group. What this is supposed to do is move an element of the group every 3500/20=175 milliseconds, but each time a different one.

 

.getAt should work.

 

I can't get your example code to work at all and I'm to lazy to code it all myself.

 

Libraries like phaser are supposed to help with that, but come on. :) I'm want to be lazy myself, but you'll have to at least give a little more effort than usual.

 

I can't get your example code to work at all and I'm to lazy to code it all myself.

game.time.events.loop(3500, function() {      this.game.add.tween(cell).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);    }, this);

I'm not a Phaser expert. Is it correct that this moves everything, every 3.5 seconds?

 

  • Do you maybe want to use cell2 instead of cell? (Better call "cell2" -> "newCell" and "cell" -> "cellGroup" or "cells" to avoid confusion.)
  • You probably also don't want to add the same event loop and set physics to arcade 20 times in a row. It has the same effect as doing it once.
  • If you don't use cell2, you can omit the "var cell2 =".

 

I would suggest to try this:

var nextMovedCell;var cell;function create() {    cell = game.add.group();    nextMovedCell = 0;    var interval = 3500/20;     for (var i = 0; i < 20; i++) {        cell.create(game.world.randomX, game.world.randomY, 'cell');    }    game.time.events.loop(interval, function() {        this.game.add.tween(cell.getAt(nextMovedCell)).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);                if (nextMovedCell < 20) {            nextMovedCell = nextMovedCell + 1;        } else {            nextMovedCell = 0;        }    }, this);    game.physics.enable(cell, Phaser.Physics.ARCADE);}

<group>.getAt(<index>) gets a single element of a group. What this is supposed to do is move an element of the group every 3500/20=175 milliseconds, but each time a different one.

 

I'm sorry if that doesn't help you. As I said, I'm not a Phaser expert and I don't know how any of the classes work. I could program this in another programming language.

 

You might want to post your whole code.

Link to comment
Share on other sites

This will get a you a random sprite from the group, and move it.

var nextMovedCell,    cells,    randomSelection;function create() {    cells = game.add.group();    nextMovedCell = 0;    var interval = 3500/20;     for (var i = 0; i < 20; i++) {        cells.create(game.world.randomX, game.world.randomY, 'cell');    }    game.time.events.loop(interval, function() {        nextMovedCell = game.rnd.integerInRange(0, 19);                this.game.add.tween(cells.getAt(nextMovedCell)).to({x: this.game.world.randomX, y: this.game.world.randomY},        19000, Phaser.Easing.Linear.InOut, true);    }, this);    game.physics.enable(cells, Phaser.Physics.ARCADE);}

To get exactly what you want you're going to have to put some work into it.

 

Half the time I don't even use what phaser provides. I don't use group methods often, and I add my own properties to sprites, and groups all over.

Link to comment
Share on other sites

  • 2 weeks later...
var nextMovedCell;var cell;function create() {    cell = game.add.group();    nextMovedCell = 0;    var interval = 3500/20;     for (var i = 0; i < 20; i++) {        cell.create(game.world.randomX, game.world.randomY, 'cell');    }    game.time.events.loop(interval, function() {        this.game.add.tween(cell.getAt(nextMovedCell)).to({x: this.game.world.randomX, y: this.game.world.randomY}, 19000, Phaser.Easing.Linear.InOut, true);                if (nextMovedCell < 20) {            nextMovedCell = nextMovedCell + 1;        } else {            nextMovedCell = 0;        }    }, this);    game.physics.enable(cell, Phaser.Physics.ARCADE);}

Hey all, i used this snippet right here and it works almost as intended. However the movement of the cells, is not random by any means. Over enough time they'll all gather in a clump in the center of the screen. I have no clue why this would happen, perhaps someone can enlighten me?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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