My define() block in scratch is not working, am I doing something wrong? - sprite

I'm working on an assignment in which I have to make a sprite move. I've drawn each frame and I want to switch costumes to make it look like it's moving.
My block looks like this:
define walking
forever
switch costume to walk1
switch costume to walk2
And my code looks like this:
when *flag* clicked
forever
if key right arrow pressed? then
change x by 10
walking
But nothing is happening.

In order to figure out how this works, let's take a look at your code actually works. Here's your code in Scratchblocks.
So if you look at that, you'll notice there are two forever loops. Here's how your code is actually running:
when flag clicked
forever
if <key [right arrow v] pressed?> then
change x by (10)
forever
switch costume to [walk1 v]
switch costume to [walk2 v]
end
end
end
See it in Scratchblocks.
It gets stuck in the switch-costume loop once you've pressed the right arrow key! By removing the forever loop from your custom block definition it should be fixed.

When you put two blocks of "switch costume to[]" they will change very fast and for you will looks like they're not changing, so to be visible you need to put one timer after each block of switch costume and removing the forever block, like this:
when flag clicked
forever
if <key [right arrow v] pressed?> then
switch costume to [walk1 v]
wait 0.5 secs
change x by (10)
switch costume to [walk2 v]
wait 0.5 secs
end
end
to a better visual effect I put the change x by (10) in the middle of them
See it in Scratchblocks

Simple fix: Remove the forever loop from the define block
define walking: ◄Fixed script
switch costume to (1)
switch costume to (2)

For even better graphics, you can do this:
When FLAG clicked
forever
if <key [right arrow v] pressed?> then
next costume
wait 0.1 secs
change x by(10)
end
end
See in scratch blocks

Related

How to properly use XMaskEvent() function?

I am programming a GUI in plain C using X11 library, on Cygwin. When trying to read cursor movements over an open window using XMaskEvent() it works like a charm. It returns an XEvent structure whose coordinates point to the current mouse location and the event_type is a small positive integer number that corresponds to what happened (mouse move, button press, button release etc). So far so good. The problem is that this command blocks (it is supposed to) after each mouse move or button event so my program cannot do anything continuous.
So I tried to replace XMaskEvent() with XCheckMaskEvent() which should read an event, remove it from the queue and continue, so that my program can continue doing something. However, XCheckMaskEvent returns an event with zero coordinates, and constant crazy event_type of minus million-something. Whatever i do with the mouse or keyboard, I am always getting an empty event of a crazy type.
I am using the same event mask for both commands.
What am I doing wrong?

Change mouse cursor when entering/exiting Area2d

I have tried every google result that seems relevant and cannot find a solution to something I'm sure is simpler than I'm making it. There's a possibility all of the results are outdated, as the official documents don't seem to offer what I'm seeing in search result suggestions.
When the mouse enters an Area2D, I want the mouse cursor to change to the hand pointing cursor. When I exit the Area2D, I want it to change back to the pointer. I am not looking to use custom cursors, just the basic ones already implemented in GODOT.
I know how to use mouse_entered/mouse_exited signals. I just can't determine the correct code and location to make the change happen.
When the mouse enters an Area2D, I want the mouse cursor to change to the hand pointing cursor. When I exit the Area2D, I want it to change back to the pointer. I am not looking to use custom cursors, just the basic ones already implemented in GODOT.
For that, the functions you want are Input.set_default_cursor_shape and Input.get_current_cursor_shape.
I know how to use mouse_entered/mouse_exited signals. I just can't determine the correct code and location to make the change happen.
You place the code in whatever signal handlers you connected. I remind you that you can connect the signals from the IDE in the "Node" panel, "Signals" tab. And in the code, you should see a green icon next to the func is connected to a signal. See also Signals.
For example, I connected them to a script in the same Area2D, and added this code:
func _on_Area2D_mouse_entered() -> void:
Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)
func _on_Area2D_mouse_exited() -> void:
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
Or if you want to store the previous CursorShape, you can do this:
var old_cursor_shape:int = Input.CURSOR_ARROW
func _on_Area2D_mouse_entered() -> void:
old_cursor_shape = Input.get_current_cursor_shape()
Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)
func _on_Area2D_mouse_exited() -> void:
Input.set_default_cursor_shape(old_cursor_shape)
See CursorShape for the values. Note: There seems to be a bug in the current beta that cause these to not autocomplete.
Reason why mouse_entered/mouse_exited might not work.
Assuming they are correctly connected.
If there is any Control (for example a Container or a background) overlapping a Node2D - such as an Area2D - regardless if it is in front or behind visually. It will prevent it from taking the input. Thus, you must set its mouse_filter of the Control to Ignore.
And make sure the collision_layer and collision_mask of the Area2D are not empty (0), input_pickable is true, and of course the Area2D has a valid CollisionShape2D or CollisionPolygon2D. Otherwise mouse_entered/mouse_exited won't work.
Also, I remind you to pay attention to any errors reported by the IDE.
One thing I recommend you can do would be to make an Area2D as a child of the root node (most likely a Node2D) rename the new Area2D to whatever you want (as long as it isn't named Area2D. For the sake of teaching you, I'm gonna rename it to CursorBox) and attach a script to that new Area2D. Add a CollisionShape2D node as a child of the Area2D, set the shape as a circle, and set the radius to 0.5. Next, go to the top right and press the node button. Connect "Area Entered' and "Area Exited" to the CursorBox. Inside of your script, make sure your functions look like what I have written below.
extends Area2D
var mouseCheck = false
func _on_CursorBox_area_entered(area):
if area.name == ("Area2D"): #This `Area2D` its looking for would be the buttons Area2D
mouseCheck = true
func _on_CursorBox_area_exited(area):
if area.name == ("Area2D"):
mouseCheck = false
The mouse check is just there to see if it works, obviously put your code in there, whatever you need to put in. I would imagine you have some animation you want to play or something. If you have any more questions, just reply and we'll see what else we can do to fix it.

Randomize SVG When Printing (On Physical Paper)

I'm curious as to whether there is a method for what I want to do. For instance: Let's say I have an SVG and decide to animate it, and every time it's run, the stroke-offset I have set is randomized, and infinite.
An example of the stroke animation is this, minus the randomization:
https://css-tricks.com/wp-content/uploads/2014/02/animate-stroke.gif
Now. If I want to open that SVG and print it (Pressing Ctrl/Cmd + P) straight from the desktop (not within any other application)— is it possible for the SVG to run it's randomization when I print it?
Again, I understand some may comment—"Print is dead, what are you doing..." But this is out of curiosity.
Print isn't dead. It just sat back and watched as we fiddled with our new toys.
Using Javascript, you can detect when printing is happening by adding a listener to the beforeprint event. See: Detecting browser print event
Then stop the animation (by adding a class, for example), and set your stroke-dashoffset to whatever you want.
Then reset everything back afterwards by listening for the afterprint event.

How can add space-key to jump for Platform behavior in construct 2?

I'm using the Platform Behavior in Construct 2 for a Windows 10 game. In addition to the arrow key, I would like to use the Space bar to get the sprite to jump. Can I tell it to call into the Platform behavior somehow or get it to let me map space bar as well as up arrow to 'jump'?
You certainly can. You can use an action called "Simulate Control" to simulate pressing left, right, or up. So as your event, use Keyboard->on Key Pressed (Space Bar). Then for the action Player->Simulate Control (Up Arrow). This will allow you to press space bar to have your character jump. Additionally, you can turn off default controls for the platform behavior and do custom controls for left, right, and up using the same strategy.
The best way to go about doing this is by adding an event that handles the Space Button. Make sure to add the keyboard object in your layout, and in the event sheet, you want to make an event that handles the keyboard -> on key pressed (choose the spacebar when asked) -> choose whatever object you want to jump with the Platform behavior -> Set Vector Y to whatever increment you'd like to jump. Be sure to list this value as negative. The y-axis in C2 is inverted, so negative numbers mean up.
Hope this helps!

How to create a mouseover effect on buttons in livecode?

I've been looking for a mouseover effect in LiveCode but I haven't got it yet.
I would like to get an effect somewhat like this link:
Creating a Mouseover Fade Effect with jQuery
This was created in jQuery. Can it be applied in LiveCode?
mouseEnter (followed by mouseLeave) is pretty much the equivalent of mouseover in web technologies. LiveCode doesn't have a built-in fade effect, but you can write your own by changing the blendLevel property of an object. For example, add the following script to a button to make it fade out to 80% translucency on mouseEnter (mouseover), and fade back to opaque on mouseLeave:
on mouseEnter
repeat with N = 0 to 80 step 5
set blendLevel of me to N
end repeat
end mouseEnter
on mouseLeave
repeat with N = 80 to 0 step -5
set blendLevel of me to N
end repeat
end mouseLeave
If you don't need the fade-in/out you can accomplish the rollover effect without scripting. Simply import the two images, then set the icon property of a button to the id of one image and the hoverIcon property of the same button to id of the other image.
In terms of scripting rollover effects, Scott's answer is spot-on—you can use the mouseEnter and mouseLeave messages to create whatever visual change you want. If you need the fade-in/out effect this will do it:
-Create a new button, make it transparent, showName off, borders off.
-Import your two images. Let's say they are named image1 and image2.
-Set the script of the button to the following:
on mouseEnter
set the effectRate to 500
lock screen for visual effect
set the icon of me to "image2"
unlock screen with visual effect dissolve
end mouseEnter
on mouseLeave
set the effectRate to 500
lock screen for visual effect
set the icon of me to "image1"
unlock screen with visual effect dissolve
end mouseLeave
This creates an effect almost precisely like the one in the link you provided. You can change the rate of the dissolve effect by changing the effectRate; higher rate for slower dissolve, lower rate for faster dissolve. (The effectRate is in milliseconds.)
on mousewithin
put random(1000)
end mousewithin

Resources