The 2DIY script archive

A resource containing Actionscript examples for 2Simples 2DIY software.

  • Home
  • ActionScript
  • Animation Script
  • Collision Script
  • Start Button Script
  • Examples
  • Ideas
  • Help
  • Discuss

More Collision Codes

Direct from Dan at 2Simple;

You can add code which will take effect when a monster collides with the player.  You can, however, use the code below to add collision code between any 2 objects of your choice. For example, drag the top sun to the canvas and add the following to the animation code of any object : 

if (_root.distBetween(_root.player,_root.s22) < _root.hitDist) { _root.player._xscale ++; _root.player._yscale ++; }

This means that whenever the player collides with the top sun, the player will increase in size. The function "distbetween" calculates the distance between the 2 objects that it is given. "hitDist" is one of the variables defined in the initial section of your code and defaults to 40. 

You could add collision code which checks for collisions against multiple objects - for example

for (n=22; n<=31; n++) {

ob = eval('_root.s' add n);

if (_root.distBetween(_root.player,ob) < _root.hitDist) { _root.player._xscale ++; _root.player._yscale ++; }

};

The above code checks whether the player has collided with any of the suns.

- Note - change "player" to "car" for the journey game.

- Note - for Collecting and Journey the top sun is s22. For Maze, Platform and Snake the top sun is s23. 

Posted by webmaster on 07/31/2009 in Collision Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

Losing a life during an edge collision

If you have been trying to create your own "Pong" style game, then you will want one side of the screen to cause the activity to lose a life, rather than bounce back an element. If you have followed the previous advice, you may have added the following code to a transparent sun element;

_root.car._x +=_root.h_velocity;

_root.car._y +=_root.v_velocity;

if (_root.car._x > 610 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 590;}

if (_root.car._x < 30 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 50;}

if (_root.car._y > 450 ) { _root.v_velocity = -_root.v_velocity; _root.car._y = 430;}

if (_root.car._y < 30 ) {_root.v_velocity = -_root.v_velocity; _root.car._y = 50;}

You need to decide which wall will not return the element, and alter that line. For example, if you wished to make the left side of the screen the area in which to prevent the element from reaching because it would result in losing a life, you would need to alter the line saying

if (_root.car._x < 30 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 50;}

(because this line has the condition  x < 30 (ie 30 pixels from the left edge). Rather than tell the activity to bounce the ball back ( the { _root.h_velocity = -_root.h_velocity; _root.car._x = 50;} part) you need to indicate that a life should be lost and so replace the code between the { and } with

_root.looseLife(); _root.car._x = 200; _root.car._y = 200;

making the whole line of code for the left edge of the screen look like this;

if (_root.car._x < 30 ) {_root.looseLife(); _root.car._x = 200; _root.car._y = 200;}

This code removes a life from the total available, and also restarts a new life in the position given with the x and y coordinates (in this case, 200 pixles across the screen and 200 down).

Posted by webmaster on 07/26/2009 in Collision Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

Edge of screen collisions

An earlier post about making a "Pong" style game, explained how it was possible (albeit in a very long and tedious way) to create the effect of a ball bouncing of a target using many, many monster elements. Thanks to Dan at 2Simple, there is a much easier way that creates the same effect.

If you were attempting to create a bounce when an object reached the edge of the screen, firstly add a sun element and make it transparent. Any code you give the sun element will be exectuted, but the graphic will not show on screen.

Within the sun element add the following code;

_root.car._x +=_root.h_velocity;

_root.car._y +=_root.v_velocity;

if (_root.car._x > 610 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 590;}

if (_root.car._x < 30 ) { _root.h_velocity = -_root.h_velocity; _root.car._x = 50;}

if (_root.car._y > 450 ) { _root.v_velocity = -_root.v_velocity; _root.car._y = 430;}

if (_root.car._y < 30 ) {_root.v_velocity = -_root.v_velocity; _root.car._y = 50;}

The first two lines define the movement effect when the object hits the wall (h and v are horizontal and vertical speeds (v for velocity). The last four lines tell the activity to reverse the direction of velocity when the element passes a certain pixel value (in the case when the object is less than 30 or greater than 610 pixels across the screen, less than 30 or more than 450 pixels down the screen.

In addition to the sun code - you will also need to add 

var v_velocity:Number = 10;

var h_velocity:Number = 7;

to the start game code (by right clicking on the green triangle). These codes set the initial values for the velocity variables used in the sun animation code.

Posted by webmaster on 07/26/2009 in Collision Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

Create a repelling object

In a journey activity, you can make a vehicle bounce off an object when it collides.

Within a journey activity select a "monster" and create an image. Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the monster. Click on the third option (what to do during a collision) and click on the "advanced" option. In the script box that appears, enter the following code;
_root.car._rotation+=30;

When your vehicle reaches the image it will cause the vehicle to bounce off in another direction

View an example; Download Repel

Posted by webmaster on 03/19/2009 in Collision Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

Cause an Oil Slick

In a journey activity, you can make a vehicle act as if it had driven onto an oil slick on the road.

Within a journey activity select a "monster" and create an oil slick image. Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the monster. Click on the third option (what to do during a collision) and click on the "advanced" option. In the script box that appears, enter the following code;

_root.car._rotation+=Math.random()*30-15;

When your vehicle passes over the slick it will cause a collision and create a fun effect

View example; Download Oil_slick

Posted by webmaster on 03/19/2009 in Collision Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

More Articles »
login

Recent Posts

  • Luke's Shark Attack
  • Lois and Paige's 2DIY drag game
  • Ellie-Jade and Jordan's Drag game!
  • Alfie and Savannah's Drag game
  • Chinese New Year
  • Haiti Earthquake Activities
  • Year 6 Little Red Riding Hood game
  • More from Sonning Common Primary
  • Collaborative learning and playing
  • Simon Haughton's 2DIY Planning

Recent Comments

  • webmaster on Discuss
  • Angela Canas on Discuss
  • webmaster on Discuss
  • Angela Canas on Discuss
  • webmaster on Discuss
  • Ali on Discuss
  • webmaster on Discuss
  • Max Wainewright on Discuss

Archives

  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009

More...

2simpleant's Blog

| The 2DIY script archive |

Design by @xannov