GameMaker: Studio

GameMaker: Studio

查看统计:
Diveyoc 2016 年 8 月 23 日 上午 12:07
[ RESOLVED ] Key Press / End Step question
My player has a mining tool that emits a beam a short distance in front of it. The instance create for the beam is activated with the key press "shift" function.
It all works great unless I am moving or rotating. The beam just stays in one spot and doesn't move with my player.
I assume I need to add an End Step code that only gets run when the "shift" key is pressed, so that the beam follows my player just like weapons do.

I've tried a few variations of the code below with no success.
Seems like the error is just in line 1.

if keyboard_check(ord('SHIFT'))
{
my_beam.x = (x + lengthdir_x(25, 2 + image_angle));
my_beam.y = (y + lengthdir_y(25, 2 + image_angle));

my_beam.image_angle = image_angle
}

I also tried using this code with no luck.
keyboard_key_press(vk_space);
最后由 Diveyoc 编辑于; 2016 年 8 月 24 日 下午 9:46
< >
正在显示第 1 - 12 条,共 12 条留言
Hakase 2016 年 8 月 23 日 上午 6:52 
Here is a complete list of the vk_ constants:
Constant Description
vk_nokey keycode representing that no key is pressed
vk_anykey keycode representing that any key is pressed
vk_left keycode for left arrow key
vk_right keycode for right arrow key
vk_up keycode for up arrow key
vk_down keycode for down arrow key
vk_enter enter key
vk_escape escape key
vk_space space key
vk_shift either of the shift keys
vk_control either of the control keys
vk_alt alt key
vk_backspace backspace key
vk_tab tab key
vk_home home key
vk_end end key
vk_delete delete key
vk_insert insert key
vk_pageup pageup key
vk_pagedown pagedown key
vk_pause pause/break key
vk_printscreen printscreen/sysrq key
vk_f1 ... vk_f12 keycode for the function keys F1 to F12
vk_numpad0 ... vk_numpad9 number keys on the numeric keypad
vk_multiply multiply key on the numeric keypad
vk_divide divide key on the numeric keypad
vk_add key on the numeric keypad
vk_subtract subtract key on the numeric keypad
vk_decimal decimal dot keys on the numeric keypad
Hakase 2016 年 8 月 23 日 上午 6:52 
The following constants can only be used with keyboard_check_direct():
Constant Description
vk_lshift left shift key
vk_lcontrol left control key
vk_lalt left alt key
vk_rshift right shift key
vk_rcontrol right control key
vk_ralt right alt key
sitebender 2016 年 8 月 23 日 上午 8:06 
While Hak covered it.... there's also Google which has GML help files:

http://docs.yoyogames.com/source/dadiospice/002_reference/mouse,%20keyboard%20and%20other%20controls/keyboard%20input/

if keyboard_check(vk_shift)
{
// welcome to GameMaker
}
最后由 sitebender 编辑于; 2016 年 8 月 23 日 上午 8:06
Hakase 2016 年 8 月 23 日 上午 10:42 
I just copied whole list here :D:
Diveyoc 2016 年 8 月 23 日 下午 11:57 
That's a great list of key commands but it really doesn't help with my question.

What I'm trying to ask is this. I have created the following keyboard_check(vk_shift) command to create my instance, and as I mentioned, it works great if my player is standing still. If I move forward then stop and press shift, the instance gets created at the proper x,y coordinate. If I rotate then stop and press shift, the instance gets created at the proper x,y coordinate.
If I press shift and then move or rotate during the 5 second animation, the instance does not follow my player movement or direction.

my_beam = instance_create(x + lengthdir_x(25, 2 + image_angle), y + lengthdir_y(25, 2 + image_angle), obj_mining_beam);

my_beam.direction = image_angle;
my_beam.image_angle = image_angle;
my_beam.speed = 0;
audio_play_sound(snd_mining_beam,0,0);

So what I'm asking, in order to have my instance follow the player movement and rotation, I have 2 questions:

1) Do I need to create a step or end step event as well? This seems like an obvious "yes" but I'm not sure.
2) If yes, do I also need to add " if keyboard_check(vk_shift) " {execute code} within the step event I create?

This all seems really similar to my create bullet event except with the bullet instance, I did not need to create and end step because the bullet just speeds away at a speed of 25.
I can't do the same for my beam, because the speed, angle, etc..are unknown or may change because I move or rotate my player.

//==== Create Bullet
bullet = instance_create(x + lengthdir_x(20, 0+image_angle), y + lengthdir_y(20, 0+image_angle),obj_9mm_bullet);
bullet.direction = image_angle;
bullet.image_angle = image_angle;
bullet.speed = 25;
audio_play_sound(snd_9mm_bullet,0,0);
最后由 Diveyoc 编辑于; 2016 年 8 月 23 日 下午 11:58
Hakase 2016 年 8 月 24 日 上午 12:10 
When i make something related to screen, sprites, objects position, i place code at draw event.
At this point all objects in their needed position and don't move.
引用自 Diveyoc
If I press shift and then move or rotate during the 5 second animation, the instance does not follow my player movement or direction.
What is your animation? Sounds like you have some movings that is not actually movings, but sprites animation. It affect object's drawing, but not affect it ngle and coordinates, and your code can't work fine.
Diveyoc 2016 年 8 月 24 日 上午 12:29 
Well, I'm quite certain that it doesn't have anything to do with the fact that my object is animated.

Imagine this, you have a gun and a bullet using similar code to what I posted above. Chances are, typically your bullet sprite is just 1 frame.
Now copy and paste the frame and add a glow effect to your bullet in the 2nd frame. Now your bullet is a 2 frame animation. Nothing else changes other than your bullet speeds across the screen with a glow effect.

Using my code for the beam, if I change the speed = 25. My mining tool will shoot the beam across the screen just like a bullet, but that's not what I want. I just want it to redraw the beam in a stationary position every frame to mirror the lengthdir coordinates that I've entered.

Based on my current situation, it wouldn't matter if I changed my mining beam to a 1 frame fixed picture. It still wouldn't follow my player. So I think the fact that it's currently animated is irrelevant.
Hakase 2016 年 8 月 24 日 上午 12:32 
Can you make gif or video of error?
Diveyoc 2016 年 8 月 24 日 上午 12:59 
I don't think a gif will help. I'll see if I can find a basic free video recording program that I can upload a video to my youtube account.
Hakase 2016 年 8 月 24 日 上午 1:29 
I'm using gyazo, it can record short video, and auto upload and convering to mp4+gif
Diveyoc 2016 年 8 月 24 日 上午 3:17 
Actually I think I may almost have this figured out with an "alarm" event. Basically it's set so that if I press the shift key it not only draws my instance, it triggers alarm [0]. Then I set the Alarm [0] event to run my code.
I don't know the exact code to write for the alarm. My instance now draws twice, once every 2 seconds. Where as I want it to draw every frame which I thought would be = to room speed but I don't know why there's a 2 second delay in the redraw. I rewrote some things as follows:

Step Event
if keyboard_check_pressed(vk_shift)
{
my_beam = instance_create(x + lengthdir_x(25, 2 + image_angle), y + lengthdir_y(25, 2 + image_angle), obj_mining_beam);
my_beam.direction = image_angle;
my_beam.image_angle = image_angle;
my_beam.speed = 0;
audio_play_sound(snd_mining_beam,0,0);

if (alarm [0] = -1) alarm = room_speed
}

Alarm 0
my_beam.x = (x + lengthdir_x(25, 2 + image_angle));
my_beam.y = (y + lengthdir_y(25, 2 + image_angle));
my_beam.direction = image_angle;
my_beam.image_angle = image_angle;
my_beam.speed = 0;
Diveyoc 2016 年 8 月 24 日 上午 6:59 
Oh man, why does this thread keep re-copying our posts.

Well, I finally figured this out. The end step that I thought I needed kept crashing because it would look for my instance (which didn't exist until I pressed the shift key).

Turns out I stumbled across this "if instance_exists" function which did the trick.

if instance_exists(obj_mining_beam)
{
my_beam.x = (x + lengthdir_x(25, 2 + image_angle));
my_beam.y = (y + lengthdir_y(25, 2 + image_angle));
my_beam.direction = image_angle;
my_beam.image_angle = image_angle;
my_beam.speed = 0;

}
< >
正在显示第 1 - 12 条,共 12 条留言
每页显示数: 1530 50

发帖日期: 2016 年 8 月 23 日 上午 12:07
回复数: 27