PDA

View Full Version : how to stop a function



pl_031
October 20th, 2004, 05:38 PM
I want to know how to stop a function.
example:


_root.onEnterFrame = function() {
move();
}

function move() {
if (Key.isDown(Key.UP)) {
hero._y -= speed;
}
}




How can i stop the function if the hero is dead and i don't want him to move ?

APDesign
October 20th, 2004, 05:46 PM
I want to know how to stop a function.
example:


_root.onEnterFrame = function() {
move();
}

function move() {
if (Key.isDown(Key.UP)) {
hero._y -= speed;
}
}




How can i stop the function if the hero is dead and i don't want him to move ?

Wherever you are determining when the hero is dead, you just need to do

delete _root.onEnterFrame;

and the hero will stop moving.

pl_031
October 20th, 2004, 05:55 PM
how to delete it ?

Smee
October 20th, 2004, 09:56 PM
When APDesign said delete _root.onEnterFrame; he was telling you the code, it wasn't part of the sentence.

delete _root.onEnterFrame; will delete the onEnterFrame function and therefore it won't continue.

JoMan
October 20th, 2004, 10:08 PM
Oh, and by the way, instead of putting:



_root.onEnterFrame = function() {
Move();
}


try putting:



_root.onEnterFrame = Move;


cause it's tidier, and it runs a bit better (I know this from experience).





peace

Dr Warm
October 21st, 2004, 03:57 AM
yeah so have in ur collision with enemy
if(hittest or whater is true){
delete _root.onEnterFrame;
}
of course there's other ways to do this if u want other stuff in _root.onEnterFrame still to run:
then have:
if(hittest or whater is true){
charDead = true;
}
_root.onEnterFrame = function() {
if(!charDead){
Move();
}
//other functions etc.
}

pl_031
October 21st, 2004, 05:11 PM
oh hehe, it works great. I didn't know the delete thing, thx

But i have another question, maybe it seems stupid but i'm a newbie:

what does "!" is supposed to do ? dr warm wrote !charDead


oh, and sorry for my english i speak french

Smee
October 21st, 2004, 06:24 PM
! means "is not true".



if(true){
//this code will obviously run
}
if(!true){
//this code will not run
}

If whatever is in the brackets of an if statement, no matter what it is, translates to "true", then it will continue.


if(false==false){
//this code will run
}

if(!false==true){
//this code will run
}

pl_031
October 21st, 2004, 09:29 PM
ooohhhh thank you, and now can you explain me please, what does all the other means, like ||, &&, etc it will be very usefull if i know what it is.

Moopers
October 21st, 2004, 09:46 PM
well... && means and


if (statement==true && variable==1){
this will happen;
}
//if statement is true and variable equals 1, something will happen


and || means or


if (statement==true || variable==1){
this will happen
}
//if statement is true or variable equals 1, something will happen

Dr Warm
October 22nd, 2004, 06:46 AM
if u want u can write:

if(something and somethingelse){
//do something
}
and also

if(something or somethingelse){
//do something different
}
but this was out dated in flash 6 with && and ||, i think because other programming languages use them (&& and ||). but u can use the others if u think u'll understand it better

pl_031
October 22nd, 2004, 05:36 PM
yipi

jonesin
October 22nd, 2004, 05:47 PM
or you can use setInterval callbacks, in which you can start and stop a function at any time, by using clearInterval.... works just as well as onEnterFrame, if not better.