|
moving my character to where the user left-clicks... |
red-dragon
Member #7,722
August 2006
|
hey guys, Now, I will attach the code here so you can see the functions. I appologize because it is not commented or clean yet, but it's simple enough that I think most of you will have no problem figuring out what's going on. So, as it stands, what I have in main loop towards end of the program: keyboard input works, character walks up, down, left, right, and northeast (I haven't programmed in se, sw, nw yet). However, the left-click doesn't work. I mean, I don't see the sprite move at all when user left-clicks. I call all the functions that I would if the user was using the keyboard. When user left-clicks on screen northeast of character, the character should walk upto where the click took place. That is the goal. Can you guys tell me where my logic is wrong? I think I've just stared at the code too long tonight and hopefully the mistake is fairly obvious. I'll put in while loop here:
here is TIA. p.s. I realize I refer to link as mario in the code, I'll be changing that soon 'nuff EDIT: c'mon guys, I desperately need help to get started with this part. If you need to ask ?s, do so... I know you guys have worked with mouse input |
razor
Member #2,256
April 2002
|
in function said: xdir = 3;
main source said: xdir = 3
First off do you have a lot of global variables or something? What are you trying to do here? you are passing mouse_x as x and mouse_y as y... those will never be negative as far as I know (mouse positions represent the pixel position of the mouse on the screen). Whoooo Oregon State University |
brigham toskin
Member #2,213
April 2002
|
First, I have to say, this is kinda an inefficient way of handling your update loop. Every time you get input and update mario's position, you drop everything and draw it to the screen. Not only can this lead to choppy logic or animation, but if you ever change the way that you render to the screen, now you've got 3 places you have to rewrite all that code. It would be better to get your input and update mario's position, and then at the end draw to the screen whatever his revised position is. Better still would be putting logic on a timer to ensure consisten speed, but for this simple example that's probably not important yet. Second, saying (x == abs(x)) is the same as saying (!(X < 0)). abs() will always return a positive version of the value fed in, so basically you're just asking to make sure it's not negative in the first place. Seems a little more straghtforward that way. Same goes for the other test, just in reverse. And third, from what I can see here, you're only updating mario's position once when it is clicked? At most he would move one step in the direction of the coords clicked. What you would have to do is move the declarations for mx and my outside of the if(mouse_B) statement so you can store them for later reference. After the code inside the if collects the mouse coords, you have a function or code block that moves mario one step toward that point at your desired speed every iteration through the loop. THEN, at the very end of the loop, you redraw the screen with Mario in his new updated position. EDIT: --- |
red-dragon
Member #7,722
August 2006
|
razor, brigham, you both bring up good points. you're right, the mouse coord will never be negative. It was late at night, and I was thinking of a regular x, y coordinate system Let me make a few changes and get back to your other comments. p.s. I'm changing that abs line crap to: EDIT: void calculate_position(int x, int y) { .... .. . else if((x > mario->x) && (y < mario->y))//(x==abs(x)) && (y!=abs(y))) { flag = 1; xdir = 3; ydir = 2; xnew = x - mario->x; ynew = mario->y - y; } While loop still looks the same as I originally posted. I've put in a flag and have tried to print out xnew and ynew in main.. It seems that within calculate_position, the statement ((x > mario->x) && (y < mario->y)) never hits true. The idea behind the statement is this: x and y are the mouse-clicked x, y values. So, if x > mario->x, then x has to move to the right. And if y < mario->y, then we have to go up. Am I wrong here again...? In main, xnew and ynew have no values... I try to print them after the for loops have executed. They are global variables, so after calculated_position gets executed, these values shouldn't change, right? SO, why isn't that else if statement turning out to be true? I'm trying to figure it out , but ANY help is always apprecited. TIA. |
brigham toskin
Member #2,213
April 2002
|
I can't say for sure without seeing what all is going on, but keep in mind that an else if will NEVER EVEN BE EVALUATED if an if before it is true. Try setting a breakpoint at the start of calculate_position() and see where it goes and why, when you click to the northeast. --- |
Johan Halmén
Member #1,550
September 2001
|
I prefer to put a return inside the braces instead of an else if after the closing brace. It looks nicer. You said: They are global variables, so after calculated_position gets executed, these values shouldn't change, right? What do you mean by that? They do change incalculated_position and will have the changed values after that in the main() scope or in any scope. You said: In main, xnew and ynew have no values...
Shouldn't ydir be set to -2 and not 2? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
red-dragon
Member #7,722
August 2006
|
Johan, Sorry for the confusion. |
|