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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Arcade

Pages: 1 ... 14 15 [16]
226
General / Re: [SFML 2.1] Sprite animation not playing
« on: December 09, 2014, 05:16:57 pm »
This might be another case where the code you provided isn't really enough to go off of. You said you can move in the y-axis easily, but can you move both up and down without problem? Based off of what you provided, all I can really say is that if currentx*32 or currenty*32 ever get less than the animated sprite's position, you won't be able to move because great_x or great_y will be false.

My suggestion would be to just add a bunch of prints to your code to make sure your variables are what you expect them to be. Alternatively, you can learn how to use a debugger (if you aren't familiar with one already) to step through your program and look for logic errors. Perhaps you could also search the internet or these forums for resources on tile based movement. For example, maybe you can get some ideas from this thread:
http://en.sfml-dev.org/forums/index.php?topic=9639

227
General / Re: [SFML 2.1] Sprite animation not playing
« on: December 08, 2014, 06:31:47 pm »
I didn't do a vigorous code review or anything like that. Here are a few things I noticed when skimming, though.

  • The variable noKeyWasPressed doesn't seem to be used for anything.
  • Your use of atoi seems dangerous. atoi is intended to be used by c-style strings, not characters. c-style strings are required to have NUL as their last character (NUL-terminated), and that is what atoi uses to determine the end of the string. You only passed a single character so atoi might use whatever garbage memory comes after your character until it happens to hit a NUL character. Also, atoi returns 0 on error, which is probably not what you want. It would probably be better to use stringstreams or std::stoi for this conversion

There may be other problems too, but those are just the two I happened to noticed when skimming  :)

228
General / Re: [SFML 2.1] Sprite animation not playing
« on: December 08, 2014, 05:23:54 pm »
You could probably make that example a lot shorter by removing all of the tilemap and pathing stuff and just have an animatedSprite move from one end of the screen to the other. I did look through it, though, and did see some potential issues, but I'll try to focus only on the question you asked specifically.

I think you were on the right track when you posted where you thought the problem was previously. What is expected by this part of the code?

Code: [Select]
while (fabs(qy) < float(x_tileSize - as.width))
{
   animatedSprite.move(sf::Vector2f(movement.x*frameTime.asSeconds(),0));
   animatedSprite.update(frameTime);
   qy += float(movement.x)*frameTime.asSeconds();
}

I think you are intending to move the sprite by a little bit at a time until it reaches the end of the tile size. Your problem is that you need to be moving your sprite once per GAME loop, not by making another loop here to move the sprite a bunch of times. This code is moving the sprite as you wanted, but won't display anything to the screen until after the loop ends when you eventually hit window.draw(animatedSprite) (this is what I was getting at in point 3 of my last post). In essence, Your code is only showing the sprite when it is done moving, not while it is in the process of moving.

Another problem with this is that your movement will basically be one tile size per frame instead of respecting your speed. Your speed variable and frameTime aren't doing anything useful because that loop will run as fast as possible until the animatedSprite reaches x_tileSize.

Hopefully all of that made sense. Basically, in pseudo code, what you want is:
  • If not at destination,move a little bit towards destination
  • Draw frame

But what you have is:
  • Continue moving until we reach destination
  • Draw frame

229
General / Re: [SFML 2.1] Sprite animation not playing
« on: December 06, 2014, 12:00:17 am »
It would help more if you provided a full example that replicates your problem, but I'll take a stab at it anyway.

I don't really know anything about this animateSprite class, but I took a quick look at the source on the wiki. Looking at your code I see a few potential problems.

  • Why is play being called 3 times in a loop. It looks like the only thing play does is set a flag saying the animation isn't paused. If your current animation isn't changing you probably only need to call play once before any of your loops.
  • The wiki examples shows that you should be calling animatedSprite.update() somewhere. Are you doing that?(this is an example of why a more complete example of what you have would be useful for us  ;))
  • What is movement.x and movement.y? Why are you nudging the sprite by those amounts in a loop without drawing anything on the screen in between?
Hopefully one of those questions will help you find what is going wrong  ;)

230
General / Re: Making a sprite rotate in the direction of the mouse position
« on: November 21, 2014, 02:12:38 am »
This seems to be a general geometry question more than a question relating to SFML. I don't visit these forums very often, so I don't know how much the community cares about non-SFML related questions, but I'll try to give you an answer anyway  :)

First of all, you are making this too hard on yourself by trying to do a cross or dot product. This is a basic geometry problem where you know the size of two sides of a right triangle and your trying to solve for the angle. The horizontal side of your right triangle is the distance between the x coordinate of your object and the x coordinate of the mouse. The vertical side of the triangle is the distance between the y coordinate of your object and the y coordinate of the mouse. Here is a picture of what I'm talking about.



Now, to solve for the angle (theta) you use the formula:

theta = tan-1(opposide side / adjacent side)
or in our case:
theta = tan-1( (mouse.y - object.y) / (mouse.x - object.x) )

as Hapax hinted at, in C or C++ you would use the atan2 function for this:
angle = atan2(mouse.y - object.y, mouse.x - object.x);

A few more notes:
  • The result of atan2 will be in radians. You will need to convert this to degrees if you are planning on using SFML's sprite rotate function.
  • Remember that in SFML y=0 is the top of the screen and y increases as you move down. This is the opposite of how normal geometry works. Therefore you may have to multiply the y component by -1 when doing your angle calculation.
  • at 0 degrees means that the mouse is to the right of your object. Therefore, it will probably be wise to make your base object sprite be pointing to the right and then rotate it based on the angle.
  • If you are using SFML's sprite rotate function be sure to set the origin of your sprite to be the center.

Hopefully that was helpful

Pages: 1 ... 14 15 [16]
anything