Game Maker - Mouse Click image_index Checking - image

I have this sprite: spr_meteoriteLv3, which has two sub-images with index image_index 0 and 1 respectively.
I have these objects: obj_meteoriteLv3, obj_tempMeteoriteLv3, and obj_score. Object obj_meteoriteLv3 spawns from above with random position, random amount, and random sub-image. Object obj_tempMeteoriteLv3 makes obj_meteoriteLv3s spawn. When player clicks on a meteorite, the program checks the value of image_index for that object.
obj_meteoriteLv3 has these events:
Create Event: change sprite_index into spr_meteoriteLv3, and start moving downwards.
Left-pressed Mouse Event: destroy the self instance, and check image_index: if image_index == 0 then score += 5; else score -= 5).
obj_tempMeteoriteLv3 has these events:
Create Event: set Alarm 0 to 3600, set variable exist to 1, and set variable add to 1.
Alarm 0: set variable add to 0, and destroy the obj_meteoriteLv3 instance.
Alarm 1: set variable exist to 1.
Step Event: if (exist == 1) then, if (add == 1) then create instance of obj_meteoriteLv3, set variable exist to 0, and set Alarm 1 to 10.
obj_score has these events:
Create Event: set score to 0.
Draw Event: draw the value of score.
The problem is, no matter which sub-image the meteorite image_index has when clicked, the score will always be incremented by 5 points. It's like the else condition isn't working. How can I fix this? Please explain your answer. Thanks.
I add some images for better understanding. Link 1. Link 2

In obj_meteoriteLv3 it's being destroyed before it can execute the rest of the code blocks. Move "Destroy Instance" to the bottom.
In obj_tempMeteoriteLv3 both variables "add" and "exist" are not necessary, instead have-
Create Event-
alarm[0] = 3600
alarm[1] = 10
Alarm[0] Event-
destroy_instance
Alarm[1] Event
Create_instance of obj_meteoriteLv3
alarm[1] = 10

Potentially, when you click, every single meteorite is triggered.
On the left click event of the meteorite, you did not check if the cursor was on the sprite. You have to use the position_meeting function, to which you pass the mouse position and the instance to click.
it would look like :
if (position_meeting(mouse_x, mouse_y, id))
{
//your destroy code
}
Moreover, when the instance_destroy(); line is read, the following code is ignored and the program jumps to the destroy_event. William explained it well and suggested to change the order of the lines of code, but you can also change the score in the destroy event directly.
I would like to add that checking for clicks in every instance of the meteorites is not optimal for performances.
My recommendation would be to use a single object to check for clicks (your player or your meteorite spawner, for example), in which you would check for clicks, and if a meteorite is touched, you would trigger it's destroy event. And in this event, you would increment the score and check the sprite.
Your click event would look like :
with (instance_position(mouse_x, mouse_y, obj_meteoriteLv3))
{
instance_destroy();
}
and in the meteorite destroy event, you would check the image_index and change the score accordingly.
EDIT : why the score doesn't change
I believe you didn't declare the score as a global variable, but as an instance variable. So when you write "score = ..." in an other object, it creates a new score variable, unrelated to the precedent.
You have 2 options :
1 - Declare the score a global variable in the create event of obj_score:
globalvar score;
score = //your starting score
Be aware that a global variable can't be set on it's initialisation line.
2 - Change the score through the score object :
whenever the score has to change :
obj_score.score = ...

Related

GODOT - Full Attack Animation Not Playing

I have this simple game i made in Godot Game Engine, and i have implemented some animations in my game's main character.
1. Run
This is a simple run animation i've added, which is played when the character is moving
2. Idle
This animation is more like a single image which is played when the character is not moving
3. Attack
This animation is played when user presses Left Mouse Button.
I am having my issue in attack animation, when i press Left Mouse Button my Animation doesn't play, instead i get first frame of the animation and then character goes back to idle Animation.
This is how it looks like:
This is my Character's Code:
extends KinematicBody2D
var _inputVec = Vector2.ZERO
var VELOCITY = Vector2.ZERO
var LAST_INPUT = Vector2.ZERO
const MAX_SPEED = 70
const ACCELERATION = 500
const FRICTION = 500
onready var animationPlayer = $AnimationPlayer
func _ready():
print("game started!")
func _physics_process(delta):
_inputVec.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
_inputVec.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
_inputVec = _inputVec.normalized()
if _inputVec != Vector2.ZERO:
if _inputVec.x > 0:
animationPlayer.play("playerRunRight")
elif _inputVec.y < 0:
animationPlayer.play("playerRunUp")
elif _inputVec.y > 0:
animationPlayer.play("playerRunDown")
else:
animationPlayer.play("playerRunLeft")
VELOCITY = VELOCITY.move_toward(_inputVec * MAX_SPEED, ACCELERATION * delta)
LAST_INPUT = _inputVec
else:
VELOCITY = VELOCITY.move_toward(Vector2.ZERO, FRICTION * delta)
if Input.is_action_just_pressed("ui_lmb"):
if LAST_INPUT.x > 0:
animationPlayer.play("playerAttackRight")
elif LAST_INPUT.y < 0:
animationPlayer.play("playerAttackUp")
elif LAST_INPUT.y > 0:
animationPlayer.play("playerAttackDown")
else:
animationPlayer.play("playerAttackLeft")
else:
if LAST_INPUT.x > 0:
animationPlayer.play("playerIdleRight")
elif LAST_INPUT.y < 0:
animationPlayer.play("playerIdleUp")
elif LAST_INPUT.y > 0:
animationPlayer.play("playerIdleDown")
else:
animationPlayer.play("playerIdleLeft")
VELOCITY = move_and_slide(VELOCITY)
Full Project is Available at my Github Repo
Remember that _physics_process runs once per (physics) frame.
So, one frame you pressed the left mouse button, and this line got to execute:
animationPlayer.play("playerAttackRight")
But next (physics) frame, you had not just pressed the left mouse button, so this conditional is false:
if Input.is_action_just_pressed("ui_lmb"):
And then this line get to execute:
animationPlayer.play("playerIdleRight")
As a result, you only see about one frame of the "playerAttackRight" animation.
You are going to need to keep track of the current state (running, attacking, idle). The rule is that you can change from running to idle immediately, but you can only change from attacking to idle, when the attack animation ends.
You can keep track of the current state with a variable, of course. You can take input and the value of the state of the variable to decide the new state. Then separately read the state variable and decide which animation to play. You may also want to set the state variable when some animations end.
And to do something when an animation ends, you can either resource to yield:
yield(animationPlayer, "animation_finished")
Which will have your code resume after it receives the "animation_finished" signal.
Or, otherwise you can connect to the "animation_finished" signal.
By the way, you can also queue animations:
animationPlayer.queue("name_of_some_animation")
While using AnimationPlayer like you do is OK. When it gets complex, there is another tool you should consider: AnimationTree.
Create an AnimationTree node, give it your animation player, and set the root to a new AnimationNodeStateMachine. There you can create your state machine, and configure if the transition between them is immediate or at the end.
Then, from code, you can get the state machine playback object, like this:
var state_machine = $AnimationTree.get("parameters/playback")
You can ask it what the current state is with:
var state:String = state_machine.get_current_node()
Which you can use as part of the decision of which state you want to go to. And then tell it you want it to go to a different state, like this:
state_machine.travel("name_of_some_state")
And using that it will respect the transitions you set, so you do not have to worry about it in code.
You can find more information about using AnimationTree at:
Using AnimationTree
Controlling Animation States

can anybody help me to understand this animation 2d code in lua?

i am a beginner as you can see and i've been following a video tutorial on defold with lua about 2D animation since then i've been asking me some question about this code, why the local variable currentAnimation is equal to 0 a then to 1 on the code, then about self.currentAnimation, as i understand currentAnimation is a method because is an action which is called through self so the object can move right?
local currentAnimation = 0
function init(self)
msg.post(".", "acquire_input_focus")
end
function final(self)
end
function update(self, dt)
end
function on_message(self, message_id, message, sender)
end
function on_input(self, action_id, action)
if action_id == hash("right") and action.pressed == true then
if self.currentAnimation == 1 then
msg.post("#sprite", "play_animation", {id = hash("runRight")})
self.currentAnimation = 0
else
msg.post("#sprite", "play_animation", {id = hash("idle")})
self.currentAnimation = 1
end
end
if action_id == hash("left") and action.pressed == true then
if self.currentAnimation == 1 then
msg.post("#sprite", "play_animation", {id = hash("runLeft")})
self.currentAnimation = 0
else
msg.post("sprite", "play_animation", {id = hash("idle")})
self.currentAnimation = 1
end
end
end
What steps should i follow so i can change the code in a way so while i press the right arrow from the keyword the "hero" moves and when i stop pressing the "hero" stops moving because with this code it doesn't stop moving until i press again the same button, btw the second chunk of code did it myself following the first one.
Now i got another question, i want to make it jump when i pressed a designated button:
if action_id == hash("jump") then
if action.pressed then
msg.post("#sprite", "play_animation", {id = hash("heroJump")})
self.currentAnimation = 0
else
msg.post("#sprite", "play_animation", {id = hash("idle")})
end
end
With this code it doesn't jump, i have tried other code but act like a loop making the jumping animation over a over, i just want it to jump every time i pressed the designated button.
The currentAnimation is surely not a method, it is a field of a table that happens to be named self.
It is anyone's guess what purpose is achieved with the line local currentAnimation = 0, it is never used afterwards.
Apparently, it seems that the code you've provided is used to describe behavior of an object (in truth, lua table). According to manual in defold framework you implement behaviors using message passing between different objects and by subscribing listeners to event relevant to your object.
The init, final,update, on_message and, importantly, on_input are all event handlers you define for particular events. The game engine then calls those whenever it decides to do so.
While handling event of pressed button, your object uses the line
msg.post("#sprite", "play_animation", {id = hash("runRight")})
to send message to engine indicating that it should draw something and probably perform some behaviors defined elsewhere.
The above code implements character as a simple finite state automata. currentAnimation is a variable indicating current state, 1 is for standing still, 0 is for running. The code inside if operator handles the transition between states. The way you've currently done it, it'll take two keypresses to change direction of running.
Your on_input event handler receives action table, which describes the event, it then filters events handling only the right button pressed down if action_id == hash("right") and action.pressed == true then (and your addition checks for left button). According to documentation, you can also check for button being released by checking field action.released. If you want the character to stop, you should add corresponding branch in the event handler. They have a bunch of examples there. You can combine those to implement behavior you want.

unity3d - Change Image.fillAmount

I have an image acting as a health bar, and i want to give it a cistume value more than 1:
public Image healthBar;
// Use this for initialization
void Start () {
float health = 4;
healthBar.fillAmount = health;
Debug.Log (gameObject + ""+ healthBar.fillAmount);
}
Problem is no matter what value I give health, the fillAmount always goes back to 1. Is there any way to make it higher ?
fillAmount is a value of 0-1, with at 1 the full image being shown. There is no behavior yet for numbers over 1.
You could change the width of the image based on the maximum health and calculate the percentage that needs to be filled.
healthBar.rectTransform.rect.width = maxHealth * healthBarWidth;
healthBar.fillAmount = health / maxHealth;
If you don't want one bar, but multiple images like hearts that still are filled partially, you might be able to do it with a tiled image which automatically truncates at the edges.
I think you made a spelling error when creating the heath float. You call health when you declared "healt"
healthBar.fillAmount += health;
Try to do the increment in the fillAmount value, as it will assign the value you want it, and then change the code little bit in order to reach the exact number.

Getting touch events when objects have an alpha of 0 In Corona SDK

If I wanted to have an invisible box, for example, how could I get touch events if it has an alpha of 0? Or is there another way to make an invisible box.
local function invisiblebuttontouch(event)
if event.phase == 'began' then
print (event.x..","..event.y)
end
end
button = display.newRect(1,1,300,300)
button:addEventListener("touch",invisiblebuttontouch)
button.alpha = 0
It never prints out the x and y, however if I don't set the alpha to 0, then it works fine.
You need to add this line to your code:
button.isHitTestable = true
Source: http://docs.coronalabs.com/api/type/DisplayObject/isHitTestable.html
it should be noted that no callback for a target will be fired if one of the parent groups are invisible disregarding isHittestable. Also setting isHittestable of the mother group won't change that.

MATLAB: Modify getline() to remove 'end input on double-click' functionality

The matlab function getline (image processing toolbox) returns the position of a polyline (which has previously been defined with the cursor) either on a double-click or on pressing the return key or spacebar.
Due to my butter-fingers and accidentally double-clicking I want to remove the ability to end on a double-click.
What part do I need to change, or what functions should I be looking out for, I can't find out how a double click is even defined in matlab.
Cheers!
MATLAB associates "callback" functions with graphics objects, which define what to do when the mouse is clicked, keys are pressed, etc.. In getline(), the section to look at is the NextButtonDown() subfunction. This is the callback that is associated with subsequent mouse presses after the first mouse press to initiate the line. The key is that is checks the SelectionType figure property, which will be open for a double click. When that is the case, it closes the figure. So, to disable that functionality, just remove the extra case and checking logic. Here is the diff for my r2009b version:
306,310d305
< selectionType = get(GETLINE_FIG, 'SelectionType');
< if (~strcmp(selectionType, 'open'))
< % We don't want to add a point on the second click
< % of a double-click
<
322,328d316
<
< end
<
< if (~strcmp(get(GETLINE_FIG, 'SelectionType'), 'normal'))
< % We're done!
< set(GETLINE_H1, 'UserData', 'Completed');
< end
The answer provided by #JohnColby solves your problem by editing the GETLINE.m function file. Basically you comment out every line that check if a double-click was performed. This information is obtained by querying the 'SelectionType' figure property.
Alternatively, if you are like me and you hate making changes to built-in functions, then consider the following solution that doesn't involve changing any existing functions. Here is an example of how we use it:
h = addlistener(handle(gcf), 'WindowButtonDownFcn', 'PostSet', #changedWBDFcn);
[x,y] = getline();
delete(h)
plot(x,y, 'Color','r')
The idea is to create an event listener that gets triggered when the 'WindowButtonDownFcn' figure property changes. We use it to insert a function that gets called just before the previously set callback function (actually we replace the callback with our own function that calls the old one at the end).
This allows us to insert a section that checks if the call was triggered by a double-click, and simply skip such event.
This had to be done twice, because GETLINE first calls FirstButtonDown on first click, which sets NextButtonDown to be called on subsequent clicks, thus the use of the flag variable to differentiate between the two cases.
The code for the above event listener function:
function changedWBDFcn(src,ev,flag)
hFig = ev.AffectedObject; %# figure handle
currFcn = ev.NewValue; %# current callback function
delete(src); %# delete event listener
if nargin < 3, flag = false; end %# determine flag
%# hijack WindowButtonDownFcn function
set(hFig, 'WindowButtonDownFcn',{#wbdFcn,currFcn,flag})
%# callback function
function wbdFcn(o,e,currFcn,flag)
%# skip anything but single-clicks
if ~strcmpi(get(hFig,'SelectionType'),'normal')
return
end
%# evaluate previous callback function
hgfeval(currFcn) %# getline('FirstButtonDown'),getline('NextButtonDown')
%# repeat process after first click
if flag
addlistener(handle(hFig), 'WindowButtonDownFcn', ...
'PostSet', {#changedWBDFcn,true});
end
end
end

Resources