Changing A Sprite With Left Click

A

Aura_Blades13

Guest
You'll have to excuse me if I'm just being silly, but I'm new to GameMaker and I can't figure this coding out.
So far I have this:

switch (device_mouse_check_button(mouse, lmb) + sp_Pod = sprite_replace(sp_Pod, sp_Open_Pod, 1, true, false, x, y))
{
case (device_mouse_check_button(0, mb_left) + sp_Pod = (sprite_replace(sp_Pod, sp_Open_Pod, 1, true, false, x, y))): break;
case (device_mouse_check_button(0, mb_left) + sp_Open_Pod = (sprite_replace(sp_Open_Pod, sp_Pod, 1, true, false, x, y))): break;
}

It's supposed to switch the sprite back and forth when someone clicks the object with the left mouse button. However, I keep receiving an error when I try to test it, saying something is wrong with the case. I'm still learning coding, so I honestly have no idea what's going wrong. Any help would be wonderful!

-Aura
 
A

Aura

Guest
It's as simple as this in the Left Mouse Pressed event:

Code:
if (sprite_index == spr_Pod_Open) {
   sprite_index = spr_Pod_Closed;
}
else {
   sprite_index = spr_Pod_Open;
}
 

Hyomoto

Member
Your case is borked, you can't use evaluations as a case. Switch and case works as follows:
Code:
switch( expression ) {
case value :
Logic
break;
So in your case:
Code:
device_mouse_check_button(mouse, lmb) + sp_Pod = sprite_replace(sp_Pod, sp_Open_Pod, 1, true, false, x, y))
The biggest thing you've done wrong however is that this won't work at all. I'm not sure what this is supposed to accomplish but the syntax is completely wrong.
Code:
switch( device_mouse_check_button( mouse, lb ) ) {
case true:
break;
}
The best I can guess is you are trying to cram a bunch of operations into a single line, but the best I can tell you by looking at what you've written is that it won't work and you need to separate your actions out into different expressions.
 
Last edited:
A

Aura

Guest
Also sprite_replace() doesn't do what you had expected it to do. It replaces a sprite resource with an external (not loaded via the IDE) one. It is not meant to change the sprite of an instance.

Also, you don't want device_mouse_check_button() either since on Windows, a simple mouse_check_button_pressed() call would suffice. As @Hyomoto has said, your switch logic is faulty. If you really want to use one, this is the correct syntax:

Code:
if (mouse_check_button_pressed(mb_left)) {
   switch (sprite_index) {
      case spr_Pod_Closed: sprite_index = spr_Pod_Open; break;
      case spr_Pod_Open: sprite_index = spr_Pod_Closed; break;
   }
}
 
A

Aura_Blades13

Guest
Also sprite_replace() doesn't do what you had expected it to do. It replaces a sprite resource with an external (not loaded via the IDE) one. It is not meant to change the sprite of an instance.

Also, you don't want device_mouse_check_button() either since on Windows, a simple mouse_check_button_pressed() call would suffice. As @Hyomoto has said, your switch logic is faulty. If you really want to use one, this is the correct syntax:

Code:
if (mouse_check_button_pressed(mb_left)) {
   switch (sprite_index) {
      case spr_Pod_Closed: sprite_index = spr_Pod_Open; break;
      case spr_Pod_Open: sprite_index = spr_Pod_Closed; break;
   }
}
Thank you so much! This is my first time making a game, let alone coding so that's probably why it was so terribly done XD. I'm always open to learning though, so thank you for explaining it so well!
 
A

Aura_Blades13

Guest
Your case is borked, you can't use evaluations as a case. Switch and case works as follows:
Code:
switch( expression ) {
case value :
Logic
break;
So in your case:
Code:
device_mouse_check_button(mouse, lmb) + sp_Pod = sprite_replace(sp_Pod, sp_Open_Pod, 1, true, false, x, y))
The biggest thing you've done wrong however is that this won't work at all. I'm not sure what this is supposed to accomplish but the syntax is completely wrong.
Code:
switch( device_mouse_check_button( mouse, lb ) ) {
case true:
break;
}
The best I can guess is you are trying to cram a bunch of operations into a single line, but the best I can tell you by looking at what you've written is that it won't work and you need to separate your actions out into different expressions.
Okay, I think I understand. Thank you so much for helping! I've never coded before, so I'm glad there is such a wonderful community to help!
 
A

Aura_Blades13

Guest
When I first stumbled on your code I thought this was some new found coding wizardry i'd never seen before. But it sounds like you're learning the basics. If you're new to coding or just GML there's some great tutorial guys on Youtube. A couple that spring to mind are HeartBeast and Shaun spalding.

HeartBeast
https://www.youtube.com/channel/UCrHQNOyU1q6BFEfkNq2CYMA

Shaun spalding
https://www.youtube.com/channel/UCn7FE3Tx391g1tWPv-1tv7Q

Happy Coding!
New found wizardry? I wish XD It made sense to me though. I've been watching Shaun, but I'll have to check out HeartBeast. Thanks!
 
Last edited by a moderator:
Top