Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Need help rotating sprite via button

Author
Message
AJ Schaeffer
15
Years of Service
User Offline
Joined: 22nd Aug 2009
Location: Jacksonville,FL
Posted: 3rd Dec 2011 22:14
Here's whats in my main Loop


Here's my function, below and out of the main loop:


When I press the button, the sprite does not rotate.
What am I doing wrong?

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Dec 2011 22:26 Edited at: 3rd Dec 2011 22:32
Is cr# a global? If not then you probably need it to be.

Edit: I think I answered too soon. There are two solutions that I can see. One is to make cr# a global and then delete the parameter in your function. The other is to return the value cr# from the function and do something like this:
cr# = Rotate_function( cr# )

Sorry, I'm on my iPad and can't remember the function name while editing the post. Here's how you get the function to return a value.



AJ Schaeffer
15
Years of Service
User Offline
Joined: 22nd Aug 2009
Location: Jacksonville,FL
Posted: 3rd Dec 2011 22:48
I returned the value, but the part I'm confused about is
Quote: "cr# = Rotate_function( cr# )"


Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 3rd Dec 2011 23:02
Ok there are two variables both called cr# (if cr# isn't global) one is local to the cannon_down function while the other can be used anywhere except inside functions. What the above in question does is passes the cannon's rotation (retrieved by the getspriteangle() command) value into the function. This value then is copied into the local cr# to which 1 is added. The function will then return the new value of the local cr# which would then be copied back into the main one. I recommend changing the name of your local in cannon_down() to something like angle# so you don't confuse yourself.

AJ Schaeffer
15
Years of Service
User Offline
Joined: 22nd Aug 2009
Location: Jacksonville,FL
Posted: 3rd Dec 2011 23:12
But where does the
"cr# = Rotate_function( cr# )"
go?

and in my case would it be

"cr# = cannon_down( cr# )"

I also changed the local variable in my function to angle#

Would that change this:
"cr# = cannon_down( cr# )"
in any way?

Also, thanks for helping. I've only been using this program for some hours

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 4th Dec 2011 01:55
I'm on my pc now.

Quote: "But where does the
"cr# = Rotate_function( cr# )"
go?"

Yes in your case it is Cannon_down( cr# ) and it would go in the same place as the function call already. So instead of:


You have this:


Quote: "I also changed the local variable in my function to angle#

Would that change this:
"cr# = cannon_down( cr# )"
in any way?
"

Nope. Functions are small programs and any local variable created inside one can only be accessed by that function inside that function. So you could technically have 10 functions all with the same local variables and they wouldn't interfer with one another.

Quote: "Also, thanks for helping. I've only been using this program for some hours"

No problem, that's what I'm here for.

Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 4th Dec 2011 10:25 Edited at: 4th Dec 2011 10:25
Just to confuse things, you could also do this to save space...

That's how I'd do it. lol

My signature is NOT a moderator plaything! Stop changing it!
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 4th Dec 2011 10:57 Edited at: 4th Dec 2011 11:02
AJ Schaeffer, just add: global cr# in the beginning of the main file, and your original code will work, even if it's not optimized like Mobiius solution.

Functions can not access your variables unless you declare them as globals, that's why your code did not work.

To get a variable from a function, you need to do like this:



Now you got the value of sum, in the answer variable.

----------------
AGK user - novice
Did Amiga / AMOS programming in the 90's, just started programming again with AGK.

Login to post a reply

Server time is: 2024-11-22 19:49:53
Your offset time is: 2024-11-22 19:49:53