xna - randomness from spritemap in spritebatch -
im sure fixed, , have searched both high , low, traversed net both east, west, north , south no prevail...
my problem this. im in middle of trying make bejeweled clone, me started in xna. im stuck on random plotting of gems/icons/pictures.
this have.
first generated list of positions, random , rectangle:
list<vector2> platser = new list<vector2>(); random slump = new random(); rectangle bildsourcen; protected override void initialize() { (int j = 0; j < 5; j++) { (int = 0; < 5; i++) { platser.add(new vector2((i*100),(j*100))); } } base.initialize(); }
pretty straight-forward.
i have loaded texture, 5 icons/gems/pictures -> 5*100px = width of 500px.
allimage = content.load<texture2d>("icons/all");
then comes "error".
protected override void draw(gametime gametime) { graphicsdevice.clear(color.cornflowerblue); spritebatch.begin(); int x = slump.next(2); bildsourcen = new rectangle((x * 100), 0, 100, 100); (int = 0; < platser.count; i++) { spritebatch.draw(allimage, new rectangle((int)platser[i].x, (int)platser[i].y, 100, 100), bildsourcen, color.white); }
so, there code. , happens: want randomly pick part of image , plot @ given coords taken vector2-list. however, puts same image @ coords , keeps randomly replacing them, not random images same. whole board keeps flickering same icons. ie, instead of generating 15231 , keeping frozen, 1 second puts 11111 , next second puts 33333.
does understand im trying describe ? i'm @ point of pulling own hair out. cat's hair has been pulled...
thx in advance
the draw
function called once each frame. line:
int x = slump.next(2);
is generating random number (either 0 or 1 in case) each frame, hence flicker.
the line after selects sprite sprite atlas based on number (specifically specifies rectangle containing sprite). , in loop follows you're drawing multiple copies of sprite (always same image).
you should doing of game logic in update
function. function give time , want implement method of waiting amount of time pass before generate random block (so keep accumulating time passes between each update
, until reaches threshold). exact mechanics of when want generate random block you.
of course, not mention there other flaws in structure of code. bejewelled played on fixed-sized board different coloured blocks (each block represent number 1 x). location of blocks should be implicit in data structure (so don't need generate platser
list).
so game class should have like:
const int boardwidth = 10; const int boardheight = 10; int[,] board = new int[boardwidth, boardheight];
then in initialize
function should fill board
, perhaps use 0 empty space , 1 x represent colours, so:
for(int x = 0; x < boardwidth; x++) for(int y = 0; y < boardheight; y++) { board[x,y] = slump.next(1, 6); // gives 5 different sprites }
then in update
wait user input or time-out before modifying board (depending on gameplay).
then in draw
function this:
for(int x = 0; x < boardwidth; x++) for(int y = 0; y < boardheight; y++) { if(board[x,y] == 0) continue; // don't render empty space vector2 position = new vector2(100*x, 100*y); rectangle bildsourcen = new rectangle(100*(board[x,y]-1), 0, 100, 100); sb.draw(allimage, position, bildsourcen, color.white); }
Comments
Post a Comment