7 Replies - 980 Views - Last Post: 17 January 2012 - 03:30 AM

#1 hardyyanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 12-December 08

how to make 2 action together in same time? XNA

Posted 15 January 2012 - 10:58 AM

hello every body..
i am trying to make ship shoot bullet and laser..
this my code:
 public readonly static TimeSpan delay = new TimeSpan(0, 0, 0, 0, 330);
        private TimeSpan lastupdate1;
        private TimeSpan lastupdate2;
        // bullet
        public Texture2D pelurutexture;
        //laser
        public Texture2D lasertexture;

public void control(GameTime GT)
        {
            if ((Keyboard.GetState().IsKeyDown(Keys.Space) || (Keyboard.GetState().IsKeyDown(Keys.RightShift))))
            {
           if (Keyboard.GetState().IsKeyDown(Keys.Space)  && (lastupdate1 > delay))
            {
                flys[0].shoot();
              
                lastupdate1 = TimeSpan.Zero; 
            }
         else if (Keyboard.GetState().IsKeyDown(Keys.RightShift) && (lastupdate1 > delay))
            {
                flys[0].shoot2();
                lastupdate1 = TimeSpan.Zero;
            }
            }
            else  if (Keyboard.GetState().IsKeyDown(Keys.Space) && (Keyboard.GetState().IsKeyDown(Keys.RightShift)&& (lastupdate1 > delay)))
            {
                flys[0].shoot();
                flys[0].shoot2();
                lastupdate1 = TimeSpan.Zero;
            }
          
        }



if i click space, it will shoot bullet, if i clik shift, it will shoot laser.. but i can not shot 2 kind like laser and bullet at same time..
if i click space and shift together, it only shoot bullet, how to make it can shoot together ya?
thx..

This post has been edited by modi123_1: 15 January 2012 - 11:18 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: how to make 2 action together in same time? XNA

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: how to make 2 action together in same time? XNA

Posted 15 January 2012 - 11:15 AM

Please edit your post to include code tags
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: how to make 2 action together in same time? XNA

Posted 15 January 2012 - 11:21 AM

I doubt this works:
if (Keyboard.GetState().IsKeyDown(Keys.Space) && (Keyboard.GetState().IsKeyDown(Keys.RightShift)&& (lastupdate1 > delay)))
{


More likely (but I don't do XNA) is that the IsKeyDown returns multiple values that have been OR'd together. So you need to check in the same way. Something like

if (Keyboard.GetState().IsKeyDown(Keys.Space | Keys.RightShift))
{
  // then do something
}


But that is a guess based on other C# experience
Was This Post Helpful? 0
  • +
  • -

#4 hardyyanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 12-December 08

Re: how to make 2 action together in same time? XNA

Posted 16 January 2012 - 07:52 AM

thx for replying..
do u mean like this?
   if (Keyboard.GetState().IsKeyDown(Keys.Space | Keys.RightShift))
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space) && (lastupdate1 > delay))
                {
                    flys[0].shoot();

                    lastupdate1 = TimeSpan.Zero;
                }
                if (Keyboard.GetState().IsKeyDown(Keys.RightShift) && (lastupdate1 > delay))
                {
                    flys[0].shoot2();
                    lastupdate1 = TimeSpan.Zero;
                }
            }
              if (Keyboard.GetState().IsKeyDown(Keys.Space & Keys.RightShift) & (lastupdate1 > delay)) 
                {
                    flys[0].shoot();
                    lastupdate1 = TimeSpan.Zero;
                    flys[0].shoot2();
                    lastupdate1 = TimeSpan.Zero;
                }

it can not shoot 2 type directly too..
Was This Post Helpful? 0
  • +
  • -

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: how to make 2 action together in same time? XNA

Posted 16 January 2012 - 08:37 AM

You should be able to accomplish this with a couple of if checks. The following code assumes that shoot() is the method you're using for firing bullets and shoot2() is the method for firing lasers.

KeyboardState keyState = Keyboard.GetState();

if (keyState.IsKeyDown(Keys.Space))
{
    if (lastUpdate1 > delay)
    {
        flys[0].shoot();
    }
}

if (keyState.IsKeyDown(Keys.LeftShift) || keyState.IsKeyDown(Keys.RightShift))
{
    if (lastUpdate1 > delay)
    {
        flys[0].shoot2();
    }
}



Also, can I get someone to throw this over to the XNA section?

With the Update method executing 60 times per second, the code would be executed so fast that it would appear as though they were both shooting at the same time, even if they are two separate events.
Was This Post Helpful? 0
  • +
  • -

#6 hardyyanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 12-December 08

Re: how to make 2 action together in same time? XNA

Posted 16 January 2012 - 09:37 AM

View PostKilorn, on 16 January 2012 - 08:37 AM, said:

You should be able to accomplish this with a couple of if checks. The following code assumes that shoot() is the method you're using for firing bullets and shoot2() is the method for firing lasers.

KeyboardState keyState = Keyboard.GetState();

if (keyState.IsKeyDown(Keys.Space))
{
    if (lastUpdate1 > delay)
    {
        flys[0].shoot();
    }
}

if (keyState.IsKeyDown(Keys.LeftShift) || keyState.IsKeyDown(Keys.RightShift))
{
    if (lastUpdate1 > delay)
    {
        flys[0].shoot2();
    }
}



Also, can I get someone to throw this over to the XNA section?

With the Update method executing 60 times per second, the code would be executed so fast that it would appear as though they were both shooting at the same time, even if they are two separate events.


thx bro.. it almost work. now my code like this:

if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (lastupdate1 > delay)
                {
                    flys[0].shoot();
              
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.RightShift))
            {
                if (lastupdate1 > delay)
                {
                    flys[0].shoot2();
                    lastupdate1 = TimeSpan.Zero;
                }
            }


if click left shift it will shoot laser, then i click space, it can shoot bullet together with laser in delay time..
but if i ho;d the space , it shoot bullet many ammount.. not get delay time.. so i add code "lastupdate1 = TimeSpan.Zero;"
to
if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (lastupdate1 > delay)
                {
                    flys[0].shoot();
                    lastupdate1 = TimeSpan.Zero;
                }
            }


the result will be can not shoot bullet and laser in same time.. so how to make that bullet can got delay time too?
thx..
Was This Post Helpful? 0
  • +
  • -

#7 ShadowsEdge19  Icon User is offline

  • D.I.C Addict

Reputation: 142
  • View blog
  • Posts: 664
  • Joined: 16-January 10

Re: how to make 2 action together in same time? XNA

Posted 16 January 2012 - 01:08 PM

Why not have a TimeSpan lastupdate1 for bullets and a separate TimeSpan lastupdate2 for lasers? This way both can work at the same time irrespective of their own unique delays?
Was This Post Helpful? 0
  • +
  • -

#8 hardyyanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 30
  • Joined: 12-December 08

Re: how to make 2 action together in same time? XNA

Posted 17 January 2012 - 03:30 AM

View PostShadowsEdge19, on 16 January 2012 - 01:08 PM, said:

Why not have a TimeSpan lastupdate1 for bullets and a separate TimeSpan lastupdate2 for lasers? This way both can work at the same time irrespective of their own unique delays?

now i have got true answer from forums msdn, using coding like this:
   if (lastupdate1 > delay)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    flys[0].shoot();
                    lastupdate1 = TimeSpan.Zero;
                }
            }
            if (lastupdate2 > delay)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.RightShift))
                {
                    flys[0].shoot2();
                    lastupdate2 = TimeSpan.Zero;
                }
            }

thx very much.. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1