c# - XNA - Make sprite unable to go out of borders -
i've been playing little around minx , miny, can't make work. have player( box ), use mouse guide around. can cross borders player can't seen. fix that, can't figure out how.
heres code ( give complete, since not sure part of need):
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 squaregame { /// <summary> /// main type game /// </summary> public class game1 : microsoft.xna.framework.game { graphicsdevicemanager graphics; spritebatch spritebatch; public game1() { graphics = new graphicsdevicemanager(this); content.rootdirectory = "content"; } /// <summary> /// allows game perform initialization needs before starting run. /// can query required services , load non-graphic /// related content. calling base.initialize enumerate through components /// , initialize them well. /// </summary> protected override void initialize() { // todo: add initialization logic here base.initialize(); } /// <summary> /// loadcontent called once per game , place load /// of content. /// </summary> /// texture2d player; vector2 spriteposition = vector2.zero; private vector2 origin; private vector2 screenpos; protected override void loadcontent() { // create new spritebatch, can used draw textures. spritebatch = new spritebatch(graphicsdevice); player = content.load<texture2d>("models\\player"); viewport viewport = graphics.graphicsdevice.viewport; origin.x = player.width / 2; origin.y = player.height / 2; screenpos.x = viewport.width / 2; screenpos.y = viewport.height / 2; // todo: use this.content load game content here } /// <summary> /// unloadcontent called once per game , place unload /// content. /// </summary> protected override void unloadcontent() { // todo: unload non contentmanager content here } /// <summary> /// allows game run logic such updating world, /// checking collisions, gathering input, , playing audio. /// </summary> /// <param name="gametime">provides snapshot of timing values.</param> private float rotationangle; protected override void update(gametime gametime) { // allows game exit if (gamepad.getstate(playerindex.one).buttons.back == buttonstate.pressed) this.exit(); float elapsed = (float)gametime.elapsedgametime.totalseconds * 2; // todo: add update logic here rotationangle += elapsed; float circle = mathhelper.pi * 2; rotationangle = rotationangle % circle;; spriteposition.x = mouse.getstate().x; spriteposition.y = mouse.getstate().y; base.update(gametime); } /// <summary> /// called when game should draw itself. /// </summary> /// <param name="gametime">provides snapshot of timing values.</param> /// protected override void draw(gametime gametime) { graphics.graphicsdevice.clear(color.lightgoldenrodyellow); // todo: add drawing code here spritebatch.begin(); spritebatch.draw(player, spriteposition, null, color.white, rotationangle, origin, 1.0f, spriteeffects.none, 0f); spritebatch.end(); base.draw(gametime); } } }
and picture of player @ border: http://imgur.com/7dnlag3
thank in advance! :)
in update method say:
if(mouse.getstate().x > 0 && mouse.getstate().x < yourwindowwidth) spriteposition.x = mouse.getstate().x; else if (mouse.getstate().x < 0) spriteposition.x = 0; else //the mouse out right side of window. spriteposition.x = yourwindowwidth; if(mouse.getstate().y > 0 && mouse.getstate().y < yourwindowheight) spriteposition.y = mouse.getstate().y; else if (mouse.getstate().y < 0) spriteposition.y = 0; else //the mouse out bottom side of window. spriteposition.y = yourwindowheight;
hope helps.
Comments
Post a Comment