Jump to content

draw sprite in loop


MomsSpaghetti
 Share

Recommended Posts

Hello,

I want to know if there is any way to draw a certain number of sprite on for loop.

So I mean such like:

for(var x = 0; x < fieldWidth; x++){
    for(var y = 0; y < fieldHeight; y++){
        draw_sprite('image', x * 32, (y * 32) + getFieldPositionY(x,y));
    }
}

 

fieldWidth and fieldHeight changes often. No I don't want use tileSprite because I want to use some of sprite with other image and other position. So tileSprite is not a option for me.

I hope somebody could help me! :)

 

- Chinafrak

Edited by Chinafreak
mispelled title
Link to comment
Share on other sites

Hope this might help you..


function createBlock(_x,_y)
{
    sprite = game.add.sprite(_x,_y,'blocks');
    sprite.animations.add('white',[0]);//To add different Frames
    sprite.animations.add('grey',[1]);//To add different Frames
    sprite.animations.add('slot',[2]);//To add different Frames
    return sprite;
}

var gridList = [];

var ArrayLength = 12;

for (var i = 0;i<ArrayLength;i++)
        for (var j = 0;j<ArrayLength;j++)
               gridList.push(createBlock((j*widthoftile)+widthspacing,(i*heightoftile)+heightspacing));

 

Link to comment
Share on other sites

Sorry, I didn't mean that. I just mispelled the title.

What I mean is how I can draw sprite in render method with for-loop. Sadly define sprite before is pretty useless for me.

Something like that (just a dumb example) which I created in Game Maker: Studio:

SFV4hft.gif

 

The code from game maker: (It's NOT javascript code!)

 

//current_time is just a timer which its counting forward.
var width = floor(9 + (sin(current_time*0.005)*3));
var height = floor(12 + (sin(current_time*0.01)*3));

//keep same random value for every update.
random_set_seed(0); 

for(var _x = 0; _x < width; _x++){
    for(var _y = 0; _y < height; _y++){
      //draw_sprite(texture, frame index, position X, positionY);
      draw_sprite(spr_circle, irandom_range(0,1), 10 + (_x * 40), 10 + (_y * 40));
    }
}

 

So Game Maker doesn't need to define object/sprite before.Only texture which it's spr_circle.

I just want to draw bunch of sprites from for-loop (and which I can use if-condition) This is exactly what I want in phaser. But I couldn't find the possible way to get this. :(

Link to comment
Share on other sites

K, first of all, realize this is a Phaser forum so posting game maker code, without specifying what it is, is going to create a lot of confusion. 

You also need to be more specific. Is your objective simply to draw sprites with a for loop with random coordinates? Simple:

for(var _x = 0; _x < width; _x++){
    for(var _y = 0; _y < height; _y++){
var min = 100;
var max = 500; 
        game.add.sprite(game.rnd.between(min,max),game.rnd.between(min,max),'textureKey');
    }
}

But that will give you sprites in random xy coordinates between 100 and 500. It won't keep them in a grid like your GIF suggests you want them to be in.

Now we need to know if you want to continually draw sprites or just draw once. Do you want to remove all the sprites and redraw periodically, like your GIF suggests?

Link to comment
Share on other sites

1 hour ago, feudalwars said:

Now we need to know if you want to continually draw sprites or just draw once. Do you want to remove all the sprites and redraw periodically, like your GIF suggests?

Exactly! I want to continue draw sprites so I can remove and redraw them. Just like my GIF. :)

The Game Maker code is actually pretty similar from JavaScript, so it should not be a problem for users. ^^ But I changed the code for better understanding. Thanks.

Link to comment
Share on other sites

15 hours ago, Chinafreak said:

I found a way, I just use bitmap and draw the sprite on bitmap. Works pretty well. But it is okay to clear bitmap every update?

Use a render texture instead of a bitmap. It is 1000x better on performance, and it's certainly okay to draw/clear a render texture every frame :). 

In general, only use Bitmaps if you are doing advanced image manipulations, the sort of stuff you do in photoshop like hue and saturation shifts. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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