In Pencil Code, how to get the relative x,y coordinates of the default turtle?
Why does the following code not work (it returns "0,0")?
fd 100
write getxy()
How to wait for the turtle to finish moving before calling getxy()?
Use done(fn). It calls the callback fn after all animation.
Related
I would like to detect the exact location of the mouse click within the 3x3 grid displayed on the screen. How can this be done in MIT scratch? Any suggestions?
There are two ways to do this.
You could create 9 sprites, hide, and use the When this sprite clicked event...
...but it would be a whole lot of unnecessary sprites.
Or you can do the following:
As #PullJosh said, you can use the mouse x and y blocks. Just do some math:
You know that the stage goes from X: -240 to 240, Y: -180 to 180.
Just put that into some code to detect ranges, below is a link to a project that is an example of this:
This project
(Note: This assumes the grid boxes are the same size.)
when greenFlag pressed
forever
if <mouse down?> then
set (lastMouseClickX) to (mouse x)
set (lastMouseClickY) to (mouse y)
end
wait until <not<mouse down?>>
end```
This is pretty simple if you want to involve 2 variables in this:
All you really need to do is to set the mousex position to a variable and the mousey position to a variable after the sprite detects a click on it. Here is an example:
When ran, the the turn (15) degrees block doesn't work- for example:
When gf clicked
say [Waaah!]
turn (15) degrees
When gf clicked
say [Waaah!]
turn (15) degrees
The sprite would say Waaah!, but wouldn't turn.
Assuming there aren't any other scripts in your sprite which would keep it from turning (e.g. a forever loop that makes it point in a particular direction), a likely cause of this is having previously used the "set rotation style" motion block. This block changes the visual behavior of how a sprite faces based on its direction value. (That's jargon for "it changes the way a sprite looks, but not how the move-steps and if-on-edge-bounce blocks behave.")
Typically, you'll want your sprites to have the "all around" rotation style (this is the default value). Your sprite may be of the style "left-right" or "don't rotate"; both these options limit the way the sprite will face. (The former, it'll face closest to +90 or -90 degrees; the latter, it'll always face 90 degrees.) You can get your sprite back to normal by using the "set rotation style" block with the input "all around".
The Say Waaaaah! block means it will be executed forever and will not execute the next code until it is executed. Since the say block is executed forever, it will not execute the block Turn (15) degrees. You may want to put them into two seperate codes:
When Green Flag clicked
forever turn (15) degrees
When Green Flag clicked
say Waaaaah!
It isn't working because of the "say [Waaah!]" block isn't set for a specific time. Replace the "say [Waaah!]" block with a "say [Waaah!] for [2] secs" block. See if that works. :D
How can you spawn SKSpritenodes randomly inside other SKSpritenodes? Maybe using constraints (in Swift)?
For example, I would like circles to spawn in random places inside different square SKSpriteNodes.
In order to do that, it's much better to use CGRects to contain the object that you are going to spawn.
You can use arc4random_uniform to get a random value for both the X and Y coordinates of the object that you are spawning and then use CGRectContainsPoint to check if the location is inside the CGRect.
I have distributed turtles on the world having size x and I wish to increase their size to y but the I want to keep their location of their lower further point same (Check figure below). How can one accomplish this?
EDIT:
I wished to write a procedure that could be applicable for all turtle heading, that is if the turtle is heading 0 or 90 or 45. Direct math in such case could be complicated.
As Seth said, this should be relatively straight forward math. If you don't want to do the math, though, and this is only for visual purposes, you could make a new turtle shape, where the "bottom" of the shape is actually at the center. Then, when turning, it will like the turtle is turning around their bottom. When changing size, again, the "bottom" will stay in the same place.
Basically, my problem is that I spawn a movieclip at coordinates where the mouse is clicked, then the movieclip is set to fall to a certain point, which is about to y=400.
The problem is that it takes the point where it spawned as the 0,0 coordinate and does it's actions using it. For an example, if I'd click at coordinates of 250y, it would fall to 650y. Is there a method where I can take the stage coordinates and use them in the movieclip, locally?
Also, I have another problem, which I haven't gotten around at fixing yet. My movieclips are set to highlight when they're hovered over with the mouse, but they are moving to the right at a constant speed. The problem is that the place where I have to hover over to highlight the movieclip doesn't change.
You would be interested in globalToLocal and localToGlobal methods on display object.
where globalToLocal(point) would transform the point to local coordinates. In your case the point is the stage x and stage y of the mouse event.