Search code examples
mit-scratch

How to detect the x and y position of a clone?


I'm trying to make a marker that puts a number on each clone, so I can assign a life variable to each one, but I haven't been able of doing so. Any ideas?

I tried setting each clone to a different color and using it to set them to a number, but I don't know how to take the position of the clone so I can move the marker with it.


Solution

  • One way to do this would be to first create 1 variable called "Clone ID" (make sure to make it "for this sprite only" ) and write

    enter image description here

    [scratchblocks]
    when green flag clicked
    set [clone ID v] to [1]
    [/scratchblocks]
    
    [scratchblocks]
    define create clone
    create clone of [myself v]
    change [clone ID v] by (1)
    [/scratchblocks]
    

    This may not be obvious, but this code makes it so that each clone you create has a unique Clone ID(this is the reason we made the variable "for this sprite only" ) and now we can writeenter image description here

    [scratchblocks]
    when I start as a clone
    forever
    replace item (clone ID v) of [x positions] with ((x position))
    replace item (clone ID v) of [y positions] with ((y position))
    end
    [/scratchblocks]
    

    Now we have stored each of the clones positions

    if you now want to add a live count for each of them you just have to create a lives variable and a lives list and replace the x positions with the lives variable and the list with the lives list enter image description here

    [scratchblocks]
    when I start as a clone
    forever
    replace (clone ID v) of [lives] with ((lives))
    end
    [/scratchblocks]