how to change clone position in the screen at specific x and y positions? - mit-scratch

I am a beginner in scratch and I am creating a simple project in which there is a sprite and I create clones of that sprite and place them in a pyramid format.
My question - Once I create a clone, how can I move that clone to specific x and y position?

If you want to move each clone to a different position, it's rather easy to do this using a variable. Basically, you can change the variable by 1 every time a new clone is created, in a way giving the clone an "ID" until the next clone is created. You can then use this ID to tell each clone where to go.
For instance:
CREATE CLONE OF (MYSELF)
CHANGE [CLONES] BY (1)
...in one script, and in another:
WHEN I START AS A CLONE
IF [CLONES] = (1)
GO TO X: Y:
ELSE IF [CLONES] = (2)
GO TO X: Y:
etc.
This allows you to put each clone in a different location. (Afterwards, if you want to move a specific one of the clones, you can use the location of the clone as a unique identifier.)

You can use the set x to () and set y to () blocks to set the x and y positions for clones. If you want to set each clone to an individual position, you can create a "For this sprite only" variable (let's call it z). When green flag clicked, you can set z to 0, then before you create a clone, increase z by 1. The clone will inherit the value for z when it was created. You can create an array called "positions" and add the desired x position of the first clone, then the desired y position of the first clone. Then you can add the desired x position of the second clone, then the desired y position of the second clone, and so on. On the clone itself, you can go to x: (item (((z)*(2))-(1)) of [positions]) y: item ((z)*(2)) of [positions]. This will cause the clone to go to the desired x and y positions for it's z.
You can view an example here: https://scratch.mit.edu/projects/391539263/

Codes here:
When green flag clicked
create clone of myself
When i start as a clone
goto x: [] y: []
( The [ ] means where you insert number )
My scratch user: https://scratch.mit.edu/users/coco7nut/

Here's a code:
when green flag clicked
create clone of myself
set x to *insert randomised value here*
set y to *insert randomised value here*
Note that x and y is a variable
when i start as a clone
go to x position: x
go to y position: y

Related

Stop at point on Path closest to given (x, y) point while staying on Network

In my model, I require my agents to go through a layout and stop adjacent to a given object. For now, I have used a series of moveTo blocks alongside code that returns the relative (x, y) coordinates that are adjacent of my objective object.
Given that I will be working to simulate an irregular layout, I want to use a Network - I am however running into issues.
According to AnyLogic's "Agents movement inside and outside network" page, in order to use Network paths, either the start or the end point of the agent's movement must be within the Network.
I have created a Rectangular Node, where my moving agents are first initiated. Then, I have them go though a moveTo block with an (x, y, z) destination. For the (x, y) coordinates, I am using Network.getNearestPath(objectiveObjectX, objectiveObjectY, 0.0, pointOnNearestPath).
The desired result would be the agent going through:
Initial Position > objectiveObject 1 > objectiveObject 2 > objectiveObject 3 > Initial Position
With all movement staying on the Network.
However, the actual result is that my agents simply ignore the existence of the Network. My agent populations exist within the Main alongside my Network, so that isn't the issue.
I can somewhat understand why going from objectiveObject to another objectiveObject doesn't follow the path, as neither the start or end destination are nodes, but even the initial movement does not function, and I do not understand why - as it begins in a node and ends in an (x, y, z) position, which is one of the scenarios covered in AnyLogic's aforementioned agent movement page.
How would I achieve my desired results? Would I have to go through the tedious exercise of creating Nodes at every single one of my destination points?
Thank you.

Get x and y 'coordinates' from object speed and direction

I have a player object that controls like the ship in Asteroids, using speed and direction. This object is fixed in the middle of the screen, but can rotate. Movement of this object is a visual illusion as other objects move past it.
I need to get x and y coordinates of this player object, from an origin of (0, 0) at room start. x and y do not provide this info as the object does not move. Does anyone know how I can get 'fake coordinates', based on the speed and direction?
One thing to make sure is that you're not just getting x and y on their own, as that will get the current object's x and y position. Instead, make sure to reference the object you're trying to get. For example:
var objectX = myShip.x;
var objectY = myShip.y;
show_debug_message("x: " + string(objectX));
show_debug_message("y: " + string(objectY));
I think you are thinking about it wrong. You do not need "fake coordinates". Real coordinates are fine. Give the ship and asteroids/enemies whatever coordinates and velocity vectors you want; randomly generate them if the game is like Asteroids.
The coordinates do not have to be fake; it is just that when you render in your game loop, you render a particular frame of reference. If the origin is the center of the screen, when you paint an object at (x,y) paint it as though it were at (x - ship_x, y - ship_y) -- including the ship, which will be at (0,0). If you wanted to make rotation relative to the ship too, you could do the same thing with rotation.
Now, you have your question tagged as game-maker. I have no idea if game-maker lets you control how sprites are painted like this. If not then you need to maintain the real coordinates as separate properties of objects and let the official (x,y) coordinates be relative to the ship. The trouble with this is that you will have to update all of the objects everytime the ship moves. But like I said I don't know how GameMaker works -- if it is a problem maybe ask a question more specific to GameMaker.
You'll need to think what you'll use to move the ship around, but then use that code on different variables.
Normally, you'll update the x or y if you want to move the ship, but since you're not going to do that, simply use a custom variable that replaces the x and y value (like posx or posy), and use them on the code that would otherwise be used to move the ship around.

Make one sprite follow another off centre, including rotation

I'm working on a car drift game, which I want to make skid marks with using the pen.
I created a circle, radius 25 around the car that I am using as a reference for where the tires would be, and I need to get the tire sprite to follow the back end of car according to its rotation. I'm trying to adapt the formulas x'= xcosθ−ysinθ, y'= xsinθ+ycosθ but I don't know how to do it.
I set the initial x value as 0 and the y value as -25, but I know this is wrong because it doesn't stay the same when the car is moved and rotated.
Here is my project:
https://scratch.mit.edu/projects/535410396/
I got the solution from a user of the Scratch Forums. For the x value:
CarX-25*sin(CarRotation)
For the y value:
CarY-25*cos(CarRotation)

How to detect exact location of mouse click on sprite in scratch

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:

How do I set camera to chase an entity, but only in x direction?

So I have a small Sprite, and I want it to be able to jump/fly but I don't want the camera to follow its y movements, just the x movement. Is this possible?
You need to use BoundCamera class or its extensions to be able to access methods responsible for setting camera bounds.
Entity e = new Entity();
e.setPosition(pX, pY);
camera.setChaseEntity(e);
camera.setBounds(0, 0, 800, 50);
camera.setBoundsEnabled(true);
First two parameter are minimal X and Y bound position (in this case bottom left corner) Two next are maximum bound position. Which means camera`s position will not exceed 800 x and 50 y value.Change as per your requirement.Hoping it may helps you.