This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-10-24 12:28:14

kodarr
New Scratcher
Registered: 2011-10-02
Posts: 18

please help with layers

Here is the game i'm working on
http://scratch.mit.edu/projects/kodarr/2062825

My problem is i want whoever has the lowest y position to be in front. then each other sprite to fall in order depending on their y position.

Example
Player          y pos = -100
Enemy1        y pos = -120
Enemy2        y pos = -90
Enemy3        y pos = -150

in this example I would want Enemy 3 in first layer, Enemy1 in second, Player in third, and Enemy 2 in fourth. 

In scratch the only way i know how to do that is to bring to front then move back x layers.  This causes blinking of sprites when they overlap since it will show it bringing to front.

Can anyone tell me where I'm going wrong here or point me to a game that does this.
Thank you.  and I know i had a post before similar to this but I couldn't figure out how to delete it and figured maybe the subject of it was why no one would help.

Offline

 

#2 2011-10-24 12:41:32

Rexpup
Scratcher
Registered: 2010-10-17
Posts: 100+

Re: please help with layers

I have the same question, and I've been on Scratch for a while now.


http://i.imgur.com/QQH4J.gif

Offline

 

#3 2011-10-24 19:37:41

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: please help with layers

You can skip the go to front and it might work then...

Here's a method you could try:
My method I made in the last 5 minutes.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#4 2011-10-24 20:00:16

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Re: please help with layers

Well, I don't know if you're looking for this, but you could do:
[blocks]<go back( -1 )layers>[/blocks]
to go forward layers.

Offline

 

#5 2011-10-25 11:50:15

kodarr
New Scratcher
Registered: 2011-10-02
Posts: 18

Re: please help with layers

this doesn't check what layer they are on in my example there are more than 2 sprites each needing to be on the proper layer especially if more than 2 are colliding at a time.

How would i do it if 3 or even 4 of them are colliding all at the same time.  How are you sure which sprites collision code will run first to note which layer they are moving to and how do you tell what their current layer is?

Offline

 

#6 2011-10-25 15:11:41

kodarr
New Scratcher
Registered: 2011-10-02
Posts: 18

Re: please help with layers

Got it working with 2 sprites heres the code i have.  Haven't yet had the chance to test with 3 sprites.

Start of program all sprites need to be sent to front in order of their y position.  Send higher y positions forward first.
and set the sprite layers according to the order you initially sorted them.
then have a forever loop broadcasting sortsprites
on the player and on each sprite you need to check
if touching another sprite
     if sprite1layer>sprite2layer and sprite2>sprite1y
        go back 1 layers
        change sprite1layer by -1
        change sprite2layer by 1
     else
        if sprite2layer>sprite1layer and sprite1y>sprite2y
           go back -1 layers
           change sprite1layer by 1
           change sprite2layer by -1

Like I said this works for 2 sprites but I think adding the 3rd sprite will kill the way I'm sorting the inital 2 sprites so may need to rework that.  Is there an easier way than this or is this the way it needs done?  seems like a lot of computing and running that code way too often.

Offline

 

#7 2011-10-26 12:38:02

kodarr
New Scratcher
Registered: 2011-10-02
Posts: 18

Re: please help with layers

My logic seems to be a bit off.  When I add a second enemy so there are 3 sprites it works only about 1/5 of the time.  heres what i have any ideas how to modify this will help.

On player sprite
When green flag is pressed
    broadcast all front and wait

When I recieve all front
     set Enemy2Layer to 3
     broadcast E2front and wait
     set Enemy1Layer to 2
     broadcast E1front and wait
     set PlayerLayer to 1
     broadcast Pfront and wait

When I recieve Pfront
     go to front

When I recieve sortsprite
     if touching enemy1
          if enemy1layer>playerlayer and playerypos>enemy1ypos
               go back 1 layers
               change enemy1layer by -1
               change playerlayer by 1
               broadcast Layercheck
           else
               if enemy1layer<playerlayer and playerypos<enemy1ypos
                  go back -1 layers
                  change enemy1layer by 1
                  change playerlayer by -1
                  broadcast Layercheck
       if touching enemy2
          if enemy2layer>playerlayer and playerypos>enemy2ypos
               go back 1 layers
               change enemy2layer by -1
               change playerlayer by 1
               broadcast Layercheck
           else
               if enemy2layer<playerlayer and playerypos<enemy2ypos
                  go back -1 layers
                  change enemy2layer by 1
                  change playerlayer by -1
                  broadcast Layercheck

Layercheck is just seeing if any 2 values of layer variables are equal and if so it changes both by 1 up or down.  This is just so all 3 sprites don't end up with the same number.
ex: if enemy1layer = player layer and playerypos>enemy1ypos
          change enemy1layer by -1
          change playerlayer by 1
          go back 1 layers

sortsprite is being broadcasted over and over

Offline

 

Board footer