MomsSpaghetti Posted January 30, 2017 Share Posted January 30, 2017 (edited) 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 January 30, 2017 by Chinafreak mispelled title Link to comment Share on other sites More sharing options...
PhasedEvolution Posted January 30, 2017 Share Posted January 30, 2017 // inside your loop var test = game.add.sprite(x_of_sprite, y_of_sprite, 'test'); Link to comment Share on other sites More sharing options...
Kishore Posted January 30, 2017 Share Posted January 30, 2017 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 More sharing options...
MomsSpaghetti Posted January 30, 2017 Author Share Posted January 30, 2017 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: 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 More sharing options...
ForgeableSum Posted January 30, 2017 Share Posted January 30, 2017 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 More sharing options...
MomsSpaghetti Posted January 30, 2017 Author Share Posted January 30, 2017 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 More sharing options...
MomsSpaghetti Posted January 31, 2017 Author Share Posted January 31, 2017 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? Link to comment Share on other sites More sharing options...
ForgeableSum Posted February 1, 2017 Share Posted February 1, 2017 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. MomsSpaghetti 1 Link to comment Share on other sites More sharing options...
MomsSpaghetti Posted February 1, 2017 Author Share Posted February 1, 2017 Ohh, thats sound good! Thanks, I'll try! Link to comment Share on other sites More sharing options...
MomsSpaghetti Posted February 1, 2017 Author Share Posted February 1, 2017 @feudalwars works pretty good + its faster! Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts