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

#1 2013-01-07 20:24:32

paulor1c4rd0
New Scratcher
Registered: 2013-01-07
Posts: 1

How to detect sprites collisions?

I'm trying to make an object stop a movement when your "x position" is equal to "x position" of other object. But this is not working... there is other way?

PS.: They are in movement from opposite sides, in opposite directions.

from already thank you.

when gf clicked
change [defeateds]  by (0)
go to x:(250) y:(-122)
change costume to [dragon1-b]
appear
slip in (3) seconds to x: ([position x] of [ele1]) y: ([position y] of [ele1])
repeat until<([position x] of [dragao] = ([position x] of [bola]))>
   change costume to [dragon1-a]
   say [GRRRR!!!] for (0.3) seconds
   slip in (0.3) seconds to x: ([position x] of [dragao]) y: (-210)
   disappear
   change [defeateds] to ((defeateds) + (1))
   stop command
end

Offline

 

#2 2013-01-08 15:16:02

BLU_Spy
Scratcher
Registered: 2012-01-05
Posts: 1000+

Re: How to detect sprites collisions?

You could use this block:

<touching [dragon v]?>
It's in the "Sensing" section.  wink

Last edited by BLU_Spy (2013-01-08 15:16:20)


I HAVE SWITCHED ACCOUNTS! My new username is NoxSpooth.

Offline

 

#3 2013-01-08 17:24:35

iNKGaming
New Scratcher
Registered: 2012-07-18
Posts: 18

Re: How to detect sprites collisions?

Hi there -

There are a couple ways to check if the X positions are equal. First, you could assign each sprite's X Position to a variable and check if variable A is equal to variable B.

Second, you can use the block " [ X Position ] of [ Sprite ] " Which can be found under "Sensing", 5th block from the bottom, just above "Loudness" and below "Timer" Then simply check of the X position of the current sprite is equal to the position of the other sprite. (Check if it's equal using the green "Operator/Logic" blocks)

By the way, at the top of your code, you might want to make it "set defeateds to 0" rather than "change defeateds by 0", otherwise your game will not play properly after the first loss.

Happy scratching,
- iNK

Offline

 

#4 2013-01-08 19:09:39

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How to detect sprites collisions?

The problem may be that your sprites are never exactly equal to each other's x positions.  For example:
                 sprite 1    sprite 2
Frame 1:        -15             10
Frame 2:        -5               0
Frame 3:        5                -10
Frame 4:        15              -10

I'd either use the "<touching []?>" block or check for a range of values rather than a specific number, like this:

repeat until<(([abs v] of ([x position v] of [other sprite v])-(x position))) < (5)>
your movement code
end
edit: blocks look bad but I've got to go, sorry!

Last edited by MoreGamesNow (2013-01-08 19:11:09)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2013-01-08 19:15:36

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: How to detect sprites collisions?

MoreGamesNow wrote:

The problem may be that your sprites are never exactly equal to each other's x positions.  For example:
                 sprite 1    sprite 2
Frame 1:        -15             10
Frame 2:        -5               0
Frame 3:        5                -10
Frame 4:        15              -10

I'd either use the "<touching []?>" block or check for a range of values rather than a specific number, like this:

repeat until<([abs v] of (([x position v] of [other sprite v])-(x position))) < (5)>
your movement code
end
edit: blocks look bad but I've got to go, sorry!

Fixed.


http://i46.tinypic.com/35ismmc.png

Offline

 

#6 2013-01-08 19:22:55

mrn0body
Scratcher
Registered: 2012-12-11
Posts: 37

Re: How to detect sprites collisions?

have your moving script for if you are not touching something and a key is pressed


Why Fit In When You Were Born To Stand Out

Offline

 

#7 2013-01-08 21:37:57

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: How to detect sprites collisions?

ErnieParke wrote:

MoreGamesNow wrote:

The problem may be that your sprites are never exactly equal to each other's x positions.  For example:
                 sprite 1    sprite 2
Frame 1:        -15             10
Frame 2:        -5               0
Frame 3:        5                -10
Frame 4:        15              -10

I'd either use the "<touching []?>" block or check for a range of values rather than a specific number, like this:

repeat until<([abs v] of (([x position v] of [other sprite v])-(x position))) < (5)>
your movement code
end
edit: blocks look bad but I've got to go, sorry!

Fixed.

Thank you  smile

Another thing to note is that you're spending about 0.6 seconds executing commands.  That means the "repeat until" block only checks once every 0.6 seconds.  Depending on the kind of "reaction time" you want from the computer, this may interfere.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#8 2013-01-10 20:51:55

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: How to detect sprites collisions?

MoreGamesNow wrote:

ErnieParke wrote:

MoreGamesNow wrote:

The problem may be that your sprites are never exactly equal to each other's x positions.  For example:
                 sprite 1    sprite 2
Frame 1:        -15             10
Frame 2:        -5               0
Frame 3:        5                -10
Frame 4:        15              -10

I'd either use the "<touching []?>" block or check for a range of values rather than a specific number, like this:

repeat until<([abs v] of (([x position v] of [other sprite v])-(x position))) < (5)>
your movement code
end
edit: blocks look bad but I've got to go, sorry!

Fixed.

Thank you  smile

Your welcome!


http://i46.tinypic.com/35ismmc.png

Offline

 

#9 2013-01-11 18:44:49

fetchydog567
Scratcher
Registered: 2011-12-28
Posts: 500+

Re: How to detect sprites collisions?

Why is two posts asking the same thing about this post that posts, and I put a post on about that, that my post, was your post which is the same post but a little bit less information in my post, which my post has less informantion but I figured it out on that post but there is more posts with more information on what this post is all about posts which is better than my post, than your post, so this this is your post, and my post, which got the same posts that post on your post and my post.

Nothing else.

Last edited by fetchydog567 (2013-01-11 18:46:32)


http://www.minecraftanonymous.com/assets/banners/banner_51.pngJust a reminder I won't be on Scratch that much but I 'll still be here, so if i'm on something, I will be commenting back, okay! wink , PICO Team!!!!!!!!!!! SCRATCH DEADW!!! PICO HACK ACCOUTNTT@@

Offline

 

Board footer