The 2DIY Script Archive

A resource of Actionscript examples for 2Simple's 2DIY software

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

Time limited effects

When making the Robin Hood activity recently, I had the idea to create a neat "superjump" feature, as mentioned here.

I've spent the weekend thinking about this solution, and was convinced that there must be a better way to achieve it without using up two monster elements. I thought that it must be possible to...

  • Set a condition so that when a character runs over a monster element it triggers the effect
  • A timer begins that allows that effect to operate for a set amount of time
  • When the time has expired, the character returns to their normal effect

I played about with this idea, placing a simple FOR loop effect on the monster element, and adding a time variable at the start of the activity, but alas it would not work, so I asked Dan at 2Simple who sent back the simplest, most straightforward solution;

In the startup script (right clicking on the green play button) the following code is added;

var timer:int = 0;

On the monster element, in the collision script area the following code is added;

_root.jumpSpeed=-26;

_root.timer=1;

Drag a sun element onto the page and in the animation script area add the following code;

if (_root.timer > 0) {

_root.timer++;

if (_root.timer >90) {

_root.jumpSpeed=-16;

_root.timer=0;

}

}

What does all this do?

var timer:int = 0;

- this sets a variable for the timer, with an initial value of 0

_root.jumpSpeed=-26;_root.timer=1;

- when the character collides with the monster element, the jump height is increased, and the timer value changes to 1 (this is important for the code below)

if (_root.timer > 0) { _root.timer++; if (_root.timer >90) { _root.jumpSpeed=-16; _root.timer=0; } }

- the first part of this code asks a question - 'is the value of the timer is greater than 0'?, if it is then continue, otherwise check again. If the value of timer is greater than 0, then the next part of the code asks another question 'is the value of the timer greater than 90?', (the maximum value for the effect to last). If it is then reset the jump height to the original value, and reset the timer to 0 ready to be triggered again.

Note: each second during the activity takes 30 frames, so for the effect to last for 3 seconds, the maximum value of 'timer' will be 90 (3seconds x 30frames)

Download 2DIY file

This would also work with...

  • falling speed (_root.maxFallingSpeed=xx;)
  • height (_root.player._height=xx;)
  • width (_root.player._width=xx;)
  • thinning your character (_root.player._xscale=xx;)
  • Stretching your character (_root.player._yscale=xx;)
  • Invisibility (_root.player._visible=xx;)

Posted in Animation Actionscript, Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

Powerup Jumps (and more!)

Recently I created a "Robin Hood" themed game. I had an idea as I planned the activity to make the main Character (Robin) have to use a mediaeval covered cart as a trampoline in order to leap to a higher point. In the end I didn't use this idea because I ran out of elements in order to make it work, but it's a really simple procedure;

When you create a 'platform' game, there is an actionscript setting in the initial start screen options that says;

_root.jumpSpeed=-16;

If you alter this value, then it affects the height a character can jump throughout the whole activity, so this needs to be left as is. Instead, you need to use 2 monster elements.

The first monster element will be your mediaeval cart / powerup-pill / etc that causes the character to develop a superjump, but for this to happen the monster element needs a few changes making.

Click on the green arrow at the bottom of the monster element, and where it says "Lose Life" for what to do when the monster collides with the character, select the 'Advanced' option from the drop down menu and enter the actionscript jump code (as was seen on the start screen options) but alter the value. For example;

_root.jumpSpeed=-20;

Now your character will be able to jump higher, but you may not want this to remain and so a second monster element is needed further along in your game. Change the collision code (as you did a moment ago), but this time enter the same value as was at the beginning of the game to return the character to 'normal' jumping. If you make this element transparent, it will be disguised on screen, and your characters superpower will just seem to have run out when it passes over it.

Why does it is need to be a monster element I hear you asking. Because the change of jump height needs to occur only during a collision, but happen everytime the character passes over the trigger. If you choose sun elements, the jump height change would be made throughout the whole activity, and using apple elements you do not get a 'do this on a collision' option.

This would also work with...

  • falling speed (_root.maxFallingSpeed=xx;)
  • height (_root.player._height=xx;)
  • width (_root.player._width=xx;)
  • thinning your character (_root.player._xscale=xx;)
  • Stretching your character (_root.player._yscale=xx;)
  • Invisibility (_root.player._visible=xx;)

In this example below, try making "Indiana Jones" Jump, and then see what happens when he jumps using the trampoline (the grey item on the top of a building).

Full Screen

Posted in Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

More on snakes...

Since finding out a very clever way to reduce the size of the snake within the 'snake' activity, I've been buzzing with ideas of what to do with it.

This was my first attempt at creating a game that allows you to collect items that make your snake grow in length.

The green play button code contains;

var seg:Number=6;

 _root.hitDist=40;

 _root.dxy=4;

There is a sun element that also contains code;

for(n=(_root.seg);n<=20;n++) {

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

 ob._x=-20;

 };

The children to be collected are actually 'monster' elements, with the collision setting set to advanced code. You'll notice that when the 'leader' moves over a child, the score increases by 1. This was achieved by adding the following code to each child; 

_root.sc+=1;

_root.seg =_root.seg+1;

this._visible=0;

this._x=root.player._x;

this._y=root.player._y;

However, this was problematic because the first child initiated the score, then the second child made the score 1, the 3rd was 2, etc. To correct this, there was a monster element placed below the starting point of the 'head' with the following code;

_root.sc+=1;

 this._visible=0;

This began the scoring, and hid the element once it had been run over.

It all worked well, apart from the large gap between the last segment, and the new addition. This is something I am still trying to sort out.

However, in the meantime, Dan at 2Simple has been thinking how to solve this problem and come up with 3 great alternatives;

Download Dan snake 1 (.2iy file)

Download Dan snake 2 (.2iy file)

Download Dan snake 3 (.2iy file)

Based on the third example, the pied piper activity has been built.

Posted in Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

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 in Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

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 in Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

More Articles »

Recent Posts

  • Coding with 2DIY - a 6 week unit of lessons
  • 2DIY and the New Computing Curriculum
  • 2DIY with Gifted and Talented groups
  • Making characters fly (and other effects)
  • High Lawn Primary Games
  • Creative Learning
  • Gallons of Games
  • Games Pod Creations
  • Kensington Avenue Primary 'Games Pod'
  • Actionscript Tutorial No.8

Categories

  • 2DIY Examples (175)
  • Actionscript Code Names (1)
  • Animation Actionscript (9)
  • Collision Actionscript (10)
  • General (10)
  • Lesson Plans & Ideas (15)
  • Start Button Actionscript (4)
  • Tutorials (43)
See More

Search

| The 2DIY script archive |

Maintained by the Digital Learning Coordinator, Porchester Junior School, Nottingham.

The actionscript codes that are used within this archive were provided by
Max Waineright and Dan Ziskind and reproduced with their permission.