Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: move sprite in the opposite direction and bullet problem  (Read 3042 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
move sprite in the opposite direction and bullet problem
« on: June 23, 2011, 07:48:52 pm »
hello everyone

i'm trying to make an enemy sprite to move to the end of the screen which is right and then move back to the start of the screen (left), but i only could make move in one direction i cant make it move in the opposite direction.

Code: [Select]


   while (App.IsOpened())
   {
int X = sprbox.GetPosition().x;
int Y = sprbox.GetPosition().y;

if(X<=700)
sprbox.Move(100* ElapsedTime, 0);

else
sprbox.Move(-200* ElapsedTime, 0);
}


[Edit]
Quote from: "section_two"

You can do something like:

Code: [Select]
 
   ...
   int dir=1;
   ...
   while (App.IsOpened())
   {
         ...
         if(X>=700)dir=-1;
         if(X<=0)dir=1;
         sprbox.Move(100 * dir * ElapsedTime, 0);
         ...
   }



that didnt work i tried it but still didnt work.
alos i have another problem where i want if you press Space bar the bullet will move through the screen, i did that but i have to keep pressing space, however i solved that problem latter by doing this
Code: [Select]

sf::Event event
while (window.GetEvent(event))
{
    if (event.Type == sf::Event::KeyReleased && event.Key.Code == sf::Key::Space)
    {
        bullet.moveme(App,ElapsedTime);
    }
}


but the problem is i have to press space to move the bullet about 2px, so i keep pressing space to move the bullet which is not what i want.

section_two

  • Newbie
  • *
  • Posts: 19
    • View Profile
move sprite in the opposite direction and bullet problem
« Reply #1 on: June 24, 2011, 05:34:07 am »
Hi
Code: [Select]

         if(X<=700)
            sprbox.Move(100* ElapsedTime, 0);
         else
            sprbox.Move(-200* ElapsedTime, 0);



Everytime sprbox goes LEFT 200, in the next loop, X < 700 , so.....RIGHT again.


You can do something like:

Code: [Select]
 
   ...
   int dir=1;
   ...
   while (App.IsOpened())
   {
         ...
         if(X>=700)dir=-1;
         if(X<=0)dir=1;
         sprbox.Move(100 * dir * ElapsedTime, 0);
         ...
   }


Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
move sprite in the opposite direction and bullet problem
« Reply #2 on: June 24, 2011, 02:00:53 pm »
Quote from: "section_two"
Hi
Code: [Select]

         if(X<=700)
            sprbox.Move(100* ElapsedTime, 0);
         else
            sprbox.Move(-200* ElapsedTime, 0);



Everytime sprbox goes LEFT 200, in the next loop, X < 700 , so.....RIGHT again.


You can do something like:

Code: [Select]
 
   ...
   int dir=1;
   ...
   while (App.IsOpened())
   {
         ...
         if(X>=700)dir=-1;
         if(X<=0)dir=1;
         sprbox.Move(100 * dir * ElapsedTime, 0);
         ...
   }



didnt work

section_two

  • Newbie
  • *
  • Posts: 19
    • View Profile
move sprite in the opposite direction and bullet problem
« Reply #3 on: June 24, 2011, 04:48:40 pm »
it works, post the full code again.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
move sprite in the opposite direction and bullet problem
« Reply #4 on: June 24, 2011, 05:37:08 pm »
Code: [Select]
int main()
{
RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

Event Event;
bullet bullet;
App.SetFramerateLimit(60);

//----------------------------------------------------------------------------------------------------------------
Image imgbox,imghero;
Sprite sprbox,sprhero;

int HeroHP=100,EnemyHP=100     ,     HeroDmg=10,EnemyDmg=10;

imgbox.LoadFromFile("e.png");
imgbox.CreateMaskFromColor(Color(255,0,255));

imghero.LoadFromFile("h.png");
imghero.CreateMaskFromColor(Color(255,0,255));

sprbox.SetImage(imgbox);
sprhero.SetImage(imghero);

sprbox.SetPosition(20,200);
bullet.Loadfile();
//----------------------------------------------------------------------------------------------------------------



while (App.IsOpened())
{
float ElapsedTime = App.GetFrameTime();

while (App.GetEvent(Event))
{
if (Event.Type == Event::Closed)
App.Close();

// Escape key pressed
if ((Event.Type == Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();

if (Event.Type == sf::Event::KeyReleased && Event.Key.Code == sf::Key::Space)
bullet.moveme(App,ElapsedTime);
 
}

if (App.GetInput().IsKeyDown(Key::Right)) sprhero.Move(100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(Key::Left)) sprhero.Move(-100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(Key::Up)) sprhero.Move( 0, -100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(Key::Down)) sprhero.Move(0, 100 * ElapsedTime);

App.Clear();
int x=10;
bullet.DrawBullt(App);
if(EnemyHP>=0)
{
if(CheckCollision(sprhero,sprbox))
EnemyHP=EnemyHP-HeroDmg;

int BoxX = sprbox.GetPosition().x;
int BoxY = sprbox.GetPosition().y;

int HeroX = sprhero.GetPosition().x;
int HeroY = sprhero.GetPosition().y;

std::cout<< "HeroHP= " << HeroHP << "\t" <<  "EnemyHP= " << EnemyHP << "\t" <<  "X= " << BoxX << "\t" << "Y=" << BoxY << "\t"<< std::endl;

/*
//------------------------ Make enemy follow Hero -------------------------------------------------
if(BoxX<HeroX)
sprbox.Move(20 * ElapsedTime, 0);

if(BoxY<HeroY)
sprbox.Move(0, 20 * ElapsedTime);



if(BoxY>HeroY)
sprbox.Move(0, -20 * ElapsedTime);

if(BoxX>HeroX)
sprbox.Move(-20 * ElapsedTime, 0);
//------------------------ Make enemy follow Hero -------------------------------------------------
*/
int dir=1;

if(BoxX>=700)
{
dir=-1;
//sprbox.Move(100 * dir * ElapsedTime, 0);
}
if(BoxX<=20)
{
dir=1;
sprbox.Move(100 * dir * ElapsedTime, 0);
}
App.Draw(sprbox);
}


App.Draw(sprhero);
App.Display();
}
    return 0;
}

section_two

  • Newbie
  • *
  • Posts: 19
    • View Profile
move sprite in the opposite direction and bullet problem
« Reply #5 on: June 24, 2011, 06:21:34 pm »
Put "int dir=1" outside the game loop

Code: [Select]

   ...
   int dir=1;
   ...
   while (App.IsOpened())
   {
         ...
         if(X>=700)dir=-1;
         if(X<=0)dir=1;
         sprbox.Move(100 * dir * ElapsedTime, 0);
         ...
   }


Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
move sprite in the opposite direction and bullet problem
« Reply #6 on: June 24, 2011, 06:42:01 pm »
Quote from: "section_two"
Put "int dir=1" outside the game loop

Code: [Select]

   ...
   int dir=1;
   ...
   while (App.IsOpened())
   {
         ...
         if(X>=700)dir=-1;
         if(X<=0)dir=1;
         sprbox.Move(100 * dir * ElapsedTime, 0);
         ...
   }


oo how stupid of me now it works, sorry but i have been deving for 21 hours now straight and i haven't got any sleep, so sorry about the stupidity LOL.
going to get some sleep now so i wouldst make more of a fool of me self

danman

  • Hero Member
  • *****
  • Posts: 1121
    • View Profile
    • Email
move sprite in the opposite direction and bullet problem
« Reply #7 on: June 24, 2011, 07:42:49 pm »
99.9% of programming errors are between the chair and the screen  :lol:
Pointilleur professionnel