Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Alternative to (If on Edge, Bounce)
- DudmasterUItra
-
15 posts
Alternative to (If on Edge, Bounce)
Hi there!
I'm working on a cloning engine that allows for infinite clones but does not use actual clones, but uses pen.
The clones have no true “script” and they are constantly being redrawn, thus I can't use the default “If on edge, bounce” script.
Can anybody give me the raw script for that block here?
I'm working on a cloning engine that allows for infinite clones but does not use actual clones, but uses pen.
The clones have no true “script” and they are constantly being redrawn, thus I can't use the default “If on edge, bounce” script.
Can anybody give me the raw script for that block here?
- drmcw
-
1000+ posts
Alternative to (If on Edge, Bounce)
private function turnAwayFromEdge(s:ScratchSprite):Boolean {
// turn away from the nearest edge if it's close enough; otherwise do nothing
// Note: comparisions are in the stage coordinates, with origin (0, 0)
// use bounding rect of the sprite to account for costume rotation and scale
var r:Rectangle = s.getRect(app.stagePane);
// measure distance to edges
var d1:Number = Math.max(0, r.left);
var d2:Number = Math.max(0, r.top);
var d3:Number = Math.max(0, ScratchObj.STAGEW - r.right);
var d4:Number = Math.max(0, ScratchObj.STAGEH - r.bottom);
// find the nearest edge
var e:int = 0, minDist:Number = 100000;
if (d1 < minDist) { minDist = d1; e = 1 }
if (d2 < minDist) { minDist = d2; e = 2 }
if (d3 < minDist) { minDist = d3; e = 3 }
if (d4 < minDist) { minDist = d4; e = 4 }
if (minDist > 0) return false; // not touching to any edge
// point away from nearest edge
var radians:Number = ((90 - s.direction) * Math.PI) / 180;
var dx:Number = Math.cos(radians);
var dy:Number = -Math.sin(radians);
if (e == 1) { dx = Math.max(0.2, Math.abs(dx)) }
if (e == 2) { dy = Math.max(0.2, Math.abs(dy)) }
if (e == 3) { dx = 0 - Math.max(0.2, Math.abs(dx)) }
if (e == 4) { dy = 0 - Math.max(0.2, Math.abs(dy)) }
var newDir:Number = ((180 * Math.atan2(dy, dx)) / Math.PI) + 90;
s.setDirection(newDir);
return true;
}
// turn away from the nearest edge if it's close enough; otherwise do nothing
// Note: comparisions are in the stage coordinates, with origin (0, 0)
// use bounding rect of the sprite to account for costume rotation and scale
var r:Rectangle = s.getRect(app.stagePane);
// measure distance to edges
var d1:Number = Math.max(0, r.left);
var d2:Number = Math.max(0, r.top);
var d3:Number = Math.max(0, ScratchObj.STAGEW - r.right);
var d4:Number = Math.max(0, ScratchObj.STAGEH - r.bottom);
// find the nearest edge
var e:int = 0, minDist:Number = 100000;
if (d1 < minDist) { minDist = d1; e = 1 }
if (d2 < minDist) { minDist = d2; e = 2 }
if (d3 < minDist) { minDist = d3; e = 3 }
if (d4 < minDist) { minDist = d4; e = 4 }
if (minDist > 0) return false; // not touching to any edge
// point away from nearest edge
var radians:Number = ((90 - s.direction) * Math.PI) / 180;
var dx:Number = Math.cos(radians);
var dy:Number = -Math.sin(radians);
if (e == 1) { dx = Math.max(0.2, Math.abs(dx)) }
if (e == 2) { dy = Math.max(0.2, Math.abs(dy)) }
if (e == 3) { dx = 0 - Math.max(0.2, Math.abs(dx)) }
if (e == 4) { dy = 0 - Math.max(0.2, Math.abs(dy)) }
var newDir:Number = ((180 * Math.atan2(dy, dx)) / Math.PI) + 90;
s.setDirection(newDir);
return true;
}
- DudmasterUItra
-
15 posts
Alternative to (If on Edge, Bounce)
Ahh, thank you, but is there any way to do this with Scratch blocks?
- drmcw
-
1000+ posts
Alternative to (If on Edge, Bounce)

Can you detect which edge of the screen you are touching? If so you should be able to work out how to reflect the “clone” due to the direction it is pointing in.
- DudmasterUItra
-
15 posts
Alternative to (If on Edge, Bounce)
I think I've got it figured out. You can see this link for my project.
- drmcw
-
1000+ posts
Alternative to (If on Edge, Bounce)
Nice idea. You say no lag but for me it was laggy and I think using clones would in fact be faster. I only got to 160 and it was crawling. You could try reducing the costume sizes in sprite1 they are too big for the sprite and may reduce lag if you made them a better size.
- DudmasterUItra
-
15 posts
Alternative to (If on Edge, Bounce)
Nice idea. You say no lag but for me it was laggy and I think using clones would in fact be faster. I only got to 160 and it was crawling. You could try reducing the costume sizes in sprite1 they are too big for the sprite and may reduce lag if you made them a better size.
I will try, thanks. My computer is pretty beefy so I don't have as many lag problems, I will try to lower them.
- Discussion Forums
- » Help with Scripts
-
» Alternative to (If on Edge, Bounce)