XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Problem with multiple sprites

Last post 7/21/2008 11:46 PM by seVor. 4 replies.
  • 7/21/2008 1:22 PM

    Problem with multiple sprites

    Ok I am having problems with putting multiple sprites on the screen that uses the same texture.  Example:  I am creating a game where you fly a ship and shoot various lasers to blow stuff up.  Well I think I have the multiple sprites thing down by using an array, however I am using a class lvl variable to hold the X axis position of the ship and another variable for the laser.  In the update method I am making them equal each other so that the laser will look like it is coming from the ship.  But when you move the ship obviously the laser moves also along the X axis. How can make it where it will not move.  I know its probably some easy logic that I am just not seeing.

    Also if there is a completely better way to do this other than arrays please let me know!!

  • 7/21/2008 2:41 PM In reply to

    Re: Problem with multiple sprites

    seVor:

    I am using a class lvl variable to hold the X axis position of the ship and another variable for the laser. 

    Class level where? In what class? Why do you need to set a variable for the laser if it hasn't fired yet? Can you have more than one laser on-screen at a time? If not, just keep the variable null until it's needed. When you go to draw the laser sprite, if the variable for its position is null, you don't draw it.

    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 7/21/2008 6:02 PM In reply to

    Re: Problem with multiple sprites

    seVor:
    ... variable to hold the X axis position of the ship and another variable for the laser.  In the update method I am making them equal each other ... when you move the ship obviously the laser moves also
    Picture a Laser.Fire method that you call 1 time, when the player presses the fire button. Laser.Fire sets the X to the ship and sets the Y close to the turret, and, perhaps flags the laser as having been fired somehow. When you update "fired" lasers, only their Y values need to change - because during Laser.Fire, you already established X. In order to move straight up the screen (assuming a space invaders genre game), only Y needs to change after the laser has been fired.
  • 7/21/2008 6:13 PM In reply to

    Re: Problem with multiple sprites

    Sounds like the variable storing the location of your laser should be a Vector2 (so you store both the x and y positions) rather than just storing the y position.  The x and y components would both be set when the laser is fired; on subsequent updates, only change the y component.  You need one Vector2 for each laser that you will have on the screen simultaneously, so an array of Vector2's would work fine.

    Good judgment comes from experience, and experience comes from bad judgment.  -- Barry LePatner

    Sixty years ago I knew everything; now I know nothing; education is a progressive discovery of our own ignorance. -- Will Durant, American philosopher/author [1885-1981]

  • 7/21/2008 11:46 PM In reply to

    Re: Problem with multiple sprites

    it sounds like laser is a class.  Do I create an array of the class to be able to create multiple instances of that sprite. 
Page 1 of 1 (5 items) Previous Next