Charging?

Non-platform specific questions.

Charging?

Postby beefwelington » Wed Jul 19, 2006 9:51 pm

Say I've got an actor, just a red dot, and when I hold down the spacebar I want it to "charge" the dot. Depending on how long I hold the spacebar, I want the dot to have a different initial velocity when I let go of the spacebar.

Like, I hold space for one second and let go and the dot moves forward at xvelocity = 2, but if I hold it for 4 seconds and let go, the dot moves forward at xvelocity = 8.

Also, in a somewhat related question, if I've got a variable in the scripting for one action, how do I transfer the value of that variable over to another script for a different action? I'm not very familiar with C, only Java.
beefwelington
 
Posts: 13
Joined: Wed Jul 19, 2006 9:47 pm
Score: 0 Give a positive score

Postby makslane » Wed Jul 19, 2006 10:03 pm

You can try this:

1) Create the variable charge_ini_time (as Integer, Actor variable)
2) On Key Down event, put this:

Code: Select all
charge_ini_time = getTime().sec_utc;


3) On Key Up event, put this:

Code: Select all
//total_time will hold the number of seconds elapsed
int total_time = getTime().sec_utc - charge_ini_time;


4) Now, you can use the total_time value to change some other value, like velocity:

Code: Select all
xvelocity = total_time;


Read more about getTime() here:
http://game-editor.com/docs/script_refe ... tm#gettime
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby beefwelington » Wed Jul 19, 2006 10:21 pm

Thanks for the fast and helpful response!

edit: I've gotten it to work, but for some reason my red dot won't budge. I've double checked, it doesn't seem to work.
Last edited by beefwelington on Wed Jul 19, 2006 10:48 pm, edited 1 time in total.
beefwelington
 
Posts: 13
Joined: Wed Jul 19, 2006 9:47 pm
Score: 0 Give a positive score

Postby makslane » Wed Jul 19, 2006 10:30 pm

Create the variable in this way, will create a local variable, only valid in the script.

To access the variable in other scripts you can:

1) Create in the Global Code editor or
2) Create an Actor variable, using the 'Variables' button on Script Editor or
3) Create an Global variable, using the 'Variables' button on Script Editor

If you need an variable with different values between actors, like 'lives' you will want use the 'Actor variable' type
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby beefwelington » Wed Jul 19, 2006 10:51 pm

Okay, now I've gotten it to work. The script for key down was set on repeat, so it kept getting new times for the charge value. Thanks for all the help.
beefwelington
 
Posts: 13
Joined: Wed Jul 19, 2006 9:47 pm
Score: 0 Give a positive score

Postby makslane » Wed Jul 19, 2006 11:14 pm

If you use repeat in the Key Down event, you will always reinit the charge_ini_time variable and don't will get the correct time between key down and key up.

I think the right way is don't use the repeat.
So, if you keep the key pressed for 10s, you will get 10s in key up.
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score

Postby DilloDude » Wed Jul 19, 2006 11:44 pm

Another way would be to create a real variable 'charge' and on key down, repeat,
Code: Select all
charge += .5;
(or any other value) and on key up, put
Code: Select all
xvelocity = charge;
charge = 0;
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby makslane » Wed Jul 19, 2006 11:48 pm

Nice solution 8)
makslane
Site Admin
 
Posts: 3947
Joined: Sat Apr 05, 2003 6:47 pm
Score: 182 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest