arrays - positioning blocks in breakout clone -
i trying make breakout clone without totally stuck. have created xml file , referenced etc. can see when debug pulls data through intending make first level have 4 blocks , dsplaying one. (curiously displays 3rd of 4 in xml list ?!?!)
using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; using blocklocations; namespace smashblocks { public class level { block block; int totalblocks; public block[] blocks; coords2[] blocklocations; contentmanager content; public level(int totalblocks, contentmanager content) { content = content; totalblocks = totalblocks -1; blocks = new block[totalblocks]; blocklocations = content.load<coords2[]>("coords2"); block = new block(40, 20, content); (int = 0; < totalblocks; i++) { blocks[i] = block; blocks[i].position = new vector2(blocklocations[i].x, blocklocations[i].y); } } public void update(gametime gametime) { (int = 0; < totalblocks; i++) { blocks[i].update(gametime); } } public void draw(spritebatch spritebatch) { (int = 0; < totalblocks; i++) { blocks[i].draw(spritebatch); } } } } ------------------- using system; using system.collections.generic; using system.linq; using microsoft.xna.framework; using microsoft.xna.framework.audio; using microsoft.xna.framework.content; using microsoft.xna.framework.gamerservices; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using microsoft.xna.framework.media; namespace smashblocks { public class block { public vector2 position; public int width, height; //bool isalive; public rectangle rect; texture2d texture; contentmanager content; public block(int blockwidth, int blockheight, contentmanager content) { content = content; texture= content.load<texture2d>("block"); //isalive = true; width = blockwidth; height = blockheight; } public void update(gametime gametime) { rect = new rectangle((int)position.x, (int)position.y, width, height); } public void draw(spritebatch spritebatch) { spritebatch.draw(texture, position, rect, color.white); } } }
i cannot paste xml code quite confident ok copied msdn website
please me
after
for (int = 0; < totalblocks; i++) { blocks[i] = block; blocks[i].position = new vector2(blocklocations[i].x, blocklocations[i].y); }
you have array totalblocks
elements referencing same block , therefore blocks displayed @ same position. want separate instance of block
each entry in array.
(int = 0; < totalblocks; i++) { blocks[i] = new block(40, 20, content); blocks[i].position = new vector2(blocklocations[i].x, blocklocations[i].y); }
Comments
Post a Comment