Avatar billede gantz Nybegynder
12. juni 2004 - 17:08 Der er 9 kommentarer og
1 løsning

Random bevægelser på scenen

Hej med jer,
jeg har en spørgsmål angående random funktionen i flash. Jeg har et movieclip af mig selv hvor jeg går. Og det her movieclip skal så bevæges tilfældigt rundt på scenen i flydendebevægelser. og må gerne bryde scenens kant <a href="http://www.thechillerlounge.dk/test.htm">Se evt. her</a>Det skal kodes i actionscript og det ville være fedt hvis man moderat zoomede ind og ud på movieclippet.
Avatar billede pyroman Nybegynder
12. juni 2004 - 18:36 #1
//Indstillinger
moveSpeed = 4; //siger sig selv... ;)
zoomSpeed = 2; //---------||---------
minZoom = 50; //minimun zoom - normal er 100 (%)
maxZoom = 150; //maximum zoom
angleGap = 20; //dette er en sjov én - jo større jo skarpere sving - min 1, max 360
chanceOfNewDirection = 10; //sandsynlighed for at ændre retning - eks: 10 = 1 ud af 10 frames
chanceOfZoom = 6; //det samme bare med zoom

function randNum (max){
    return Math.floor(Math.random()*max);
}
MovieClip.prototype.randomMove = function(chance){
    if(randNum(chance) == 0){
        this.randDiff = randNum(_root.angleGap)-angleGap/2;
    }
    this.angle += this.randDiff;
    this.xSpeed = Math.cos(Math.PI/180*this.angle)*_root.moveSpeed;
    this.ySpeed = -Math.sin(Math.PI/180*this.angle)*_root.moveSpeed;
   
    this._x += this.xSpeed;
    this._y += this.ySpeed;
   
    if((this._x < 0 || this._x > _root.swidth) || (this._y < 0 || this._y > _root.sheight)){
        this._x -= this.xSpeed;
        this._y -= this.ySpeed;
        this.angle = -this.angle;
        this.randomMove(2);
    }
}
MovieClip.prototype.randomZoom = function(chance){
    if(randNum(chance) == 0){
        this.zoom = (randNum(3)-1)*_root.zoomSpeed;
    }
    this._xscale = this._yscale += this.zoom;
    if(this._xscale < _root.minZoom || this._xscale > _root.maxZoom){
        this._xscale = this._yscale -= this.zoom;
        this.randomZoom(1);
    }
}
MovieClip.prototype.startMove = function(){
    _root.swidth = Stage.width;
    _root.sheight = Stage.height;
    this.randDiff = 0;
    this.angle = randNum(360);
    this.onEnterFrame = function(){
            this.randomMove(_root.chanceOfNewDirection);
            this.randomZoom(_root.chanceOfZoom);
    }
}

DIT_MOVIE_CLIP.startMove();




...smart script jeg lige fik smækket sammen ;)

Spørg hvis du har problemer med at få det til at virke!

/Filip
Avatar billede gantz Nybegynder
12. juni 2004 - 19:41 #2
Hey Pyroman,
Det er præcis hvad jeg har brug for, men jeg får en fejlmeddelelse  når jeg eksporterer scriptet

256 levels of recursion were exceeded in one action list.
This is probably an infinite loop.
Further execution of actions has been disabled in this movie.

Hvad skal jeg gøre ved det?
Avatar billede pyroman Nybegynder
12. juni 2004 - 20:54 #3
dit movieclip må ikke ligge udenfor stagen til at starte med... det kan være det gør det?
Avatar billede pyroman Nybegynder
12. juni 2004 - 20:57 #4
hmm - mit script kan faktisk godt fejle... den skal jeg nok fikse ;)
Avatar billede pyroman Nybegynder
12. juni 2004 - 21:17 #5
her er en ny én :)


moveSpeed = 4;
zoomSpeed = 2;
minZoom = 50;
maxZoom = 150;
angleGap = 20;
chanceOfNewDirection = 10;
chanceOfZoom = 6;

function randNum (max){
    return Math.floor(Math.random()*max);
}
MovieClip.prototype.randomMove = function(chance){
    if(randNum(chance) == 0){
        this.randDiff = randNum(_root.angleGap)-angleGap/2;
    }
    this.angle += this.randDiff;
    if(this.angle > 180) this.angle = -360+this.angle;
    if(this.angle < -180) this.angle = 360+this.angle;

    this.xSpeed = Math.cos(Math.PI/180*this.angle)*_root.moveSpeed;
    this.ySpeed = -Math.sin(Math.PI/180*this.angle)*_root.moveSpeed;
   
    this._x += this.xSpeed;
    this._y += this.ySpeed;
   
    if((this._x < 0 || this._x > _root.swidth) || (this._y < 0 || this._y > _root.sheight)){
        this._x -= this.xSpeed;
        this._y -= this.ySpeed;
        if(angle >= 0) this.angle = -180+this.angle;
        else this.angle = 180+this.angle;
    }
}
MovieClip.prototype.randomZoom = function(chance){
    if(randNum(chance) == 0){
        this.zoom = (randNum(3)-1)*_root.zoomSpeed;
    }
    this._xscale = this._yscale += this.zoom;
    if(this._xscale < _root.minZoom || this._xscale > _root.maxZoom){
        this._xscale = this._yscale -= this.zoom;
        this.randomZoom(1);
    }
}
MovieClip.prototype.startMove = function(){
    _root.swidth = Stage.width;
    _root.sheight = Stage.height;
    this.randDiff = 0;
    this.angle = randNum(360);
    this.onEnterFrame = function(){
            this.randomMove(_root.chanceOfNewDirection);
            this.randomZoom(_root.chanceOfZoom);
    }
}
DIT_MOVIE_CLIP.startMove();
Avatar billede gantz Nybegynder
12. juni 2004 - 22:14 #6
Det er helt perfekt. Hvis du på stående fod lige kan sætte nogle maks værdier for hvor langt movieklippet må gå uden for scenene vil det være fedt. Ellers siger jeg mange tak for hjælpen.
Avatar billede pyroman Nybegynder
12. juni 2004 - 22:46 #7
moveSpeed = 4;
zoomSpeed = 2;
minZoom = 50;
maxZoom = 150;
angleGap = 20;
chanceOfNewDirection = 10;
chanceOfZoom = 6;
minx = 0;
miny = 0;
maxx = 550;
maxy = 400;

function randNum (max){
    return Math.floor(Math.random()*max);
}
MovieClip.prototype.randomMove = function(chance){
    if(randNum(chance) == 0){
        this.randDiff = randNum(_root.angleGap)-angleGap/2;
    }
    this.angle += this.randDiff;
    if(this.angle > 180) this.angle = -360+this.angle;
    if(this.angle < -180) this.angle = 360+this.angle;

    this.xSpeed = Math.cos(Math.PI/180*this.angle)*_root.moveSpeed;
    this.ySpeed = -Math.sin(Math.PI/180*this.angle)*_root.moveSpeed;
 
    this._x += this.xSpeed;
    this._y += this.ySpeed;
 
    if((this._x < _root.minx || this._x > _root.maxx) || (this._y < _root.miny || this._y > _root.maxy)){
        this._x -= this.xSpeed;
        this._y -= this.ySpeed;
        if(angle >= 0) this.angle = -180+this.angle;
        else this.angle = 180+this.angle;
    }
}
MovieClip.prototype.randomZoom = function(chance){
    if(randNum(chance) == 0){
        this.zoom = (randNum(3)-1)*_root.zoomSpeed;
    }
    this._xscale = this._yscale += this.zoom;
    if(this._xscale < _root.minZoom || this._xscale > _root.maxZoom){
        this._xscale = this._yscale -= this.zoom;
        this.randomZoom(1);
    }
}
MovieClip.prototype.startMove = function(){
    _root.swidth = Stage.width;
    _root.sheight = Stage.height;
    this.randDiff = 0;
    this.angle = randNum(360);
    this.onEnterFrame = function(){
            this.randomMove(_root.chanceOfNewDirection);
            this.randomZoom(_root.chanceOfZoom);
    }
}
DIT_MOVIE_CLIP.startMove();

Sådan..
Avatar billede gantz Nybegynder
13. juni 2004 - 13:07 #8
Hey Pyroman, det er super fedt. Du skal lige skrive et svar, så jeg kan give dig point. Tark.
Avatar billede pyroman Nybegynder
13. juni 2004 - 16:12 #9
Så lidt :)
Avatar billede gantz Nybegynder
13. juni 2004 - 16:44 #10
Tark for hjælpen...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester