Object Repulsion

Материал из Поле цифровой дидактики

Object Repulsion is the act of repulsing or repelling an object. Listed below are two methods of replicating this effect in Scratch. Шаблон:Tip

Velocity Method

The Velocity Method uses the following scripts to automatically make objects change their X and Y Velocity based upon their position. This script can go into any object that is being repelled. This method is more advanced than the next method, however this method is better in most situations.

Шаблон:Note

whenclickedgotox:0y:0setyspeedto0setxspeedto0foreverifabsofxpositionofSprite1-xpositionofSprite2<50thenifxpositionofSprite2>xpositionthenchangexspeedby-0.1endifxpositionofSprite2<xpositionthenchangexspeedby0.1endendifabsofypositionofSprite1-ypositionofSprite2<50thenifypositionofSprite2>ypositionthenchangeyspeedby-0.1endifypositionofSprite2<ypositionthenchangeyspeedby0.1endendendwhenclickedforeverifonedge,bouncechangexbyxspeedchangeybyyspeedsetxspeedto0.98*xspeedsetyspeedto0.98*yspeedend

An example can be seen here.

Direct Movement Method

The Direct Movement Method uses the scripts below to move and rotate based on its position. This method is basic, but very effective in doing its job.

Шаблон:Note

whenclickedgotox:0y:-1foreverifdistancetoSprite2<50thenpointtowardsSprite2move-10stepsifonedge,bounce

An example can be seen here.

See Also

ja:オブジェクト間の斥力