Hallo!
I am looking for an actionscript to make a kite fly randomly in the air.
Any help is appreciated.
Thanks so much!
Printable View
Hallo!
I am looking for an actionscript to make a kite fly randomly in the air.
Any help is appreciated.
Thanks so much!
This is the best I can do:
make a kite, convert it to a movieclip with the registration point set to center, give it an instance name of mc, and use this code on your Frame:
Actionscript Code:X = mc._x;
Y = mc._y
function randomPoints(){
X = Math.random()*Stage.width;
Y = Math.random()*Stage.height;
r = (Math.random()*180)-90;
}
mc.onEnterFrame = function(){
this._x += (X-this._x)/100;
this._y += (Y-this._y)/100;
this._rotation += (r-this._rotation)/50;
}
setInterval(randomPoints, 500);
not good, but better than the answer you got over at Actionscript.org :/
Thanks so much for the effort Nig, I'll give that a shot! Kudos for noticing my other post....
I thought the movements were not smooth enough either, so I gave it another shot, using trigonometry randomly :p:
Actionscript Code:speed = 0.06; // the lower the slower
wind = 5; // the higher the slower
r = 2;
d = 5;
onEnterFrame = function(){
r += speed;
d += (Math.random())/wind;
mc._x += Math.sin(r+(Math.random()*Math.cos(r/2)));
mc._y += Math.sin(r+(Math.random()*Math.tan(r)));
mc._rotation = (Math.cos(d)*90/Math.PI)/5;
}
Try adjusting the first two variables, if you want to change the speed and wind. This one is not perfect, but hopefully better than the last one :)
Slightly different approach here:
Actionscript Code:import flash.display.BitmapData;
// set the bViewWind boolean true to see a larger presentation of the
// perlin noise bitmapData
var bViewWind:Boolean = false;
var windBMD:BitmapData;
if (bViewWind){
windBMD = new BitmapData(400, 300, false, 0x000000);
this.createEmptyMovieClip("bmp1", this.getNextHighestDepth());
bmp1.attachBitmap(windBMD, 1, auto, true);
}else{
windBMD = new BitmapData(1, 1, false, 0x000000);
}
var seed:Number = Math.floor(Math.random() * 256);
var channels:Number = 1 | 2;
var offset:Array = [{x:0,y:0}];
this.createEmptyMovieClip("kite", this.getNextHighestDepth());
kite.beginFill(0xaa0000);
kite.moveTo(0,-20);
kite.lineTo(20,0);
kite.lineTo(0,40);
kite.lineTo(-20,0);
kite.endFill();
var windBluster:Number = getBluster();
var initX:Number = 50;
var initY:Number = 400;
var endX:Number = 400;
var endY:Number = 150;
this.onEnterFrame = function(){
this.offset[0].x -= 2;
this.offset[0].y += 1;
this.windBMD.perlinNoise( 400, 300, 1, seed, false, true, channels, false, offset );
var col:Number = windBMD.getPixel(0,0);
var colvals:Object = col2rgb(col);
this.kite._x = this.endX + (127 - colvals.r)/this.windBluster;
this.kite._y = this.endY + (127 - colvals.g)/this.windBluster;
this.drawLine((127 - colvals.g), (256 - colvals.r));
}
function getBluster():Number{
var val:Number = 0;
while(val == 0){
val = Math.random()*5;
}
return Math.max(0.6,val);
}
function col2rgb(hex):Object{
var red = hex>>16;
var greenBlue = hex-(red<<16)
var green = greenBlue>>8;
// no need for blue in this instance
//var blue = greenBlue - (green << 8);
return({r:red, g:green});
}
function drawLine(swingX:Number, swingY:Number){
this.clear();
this.lineStyle(1, 0);
this.moveTo(this.initX, this.initY);
this.curveTo(this.kite._x+swingX, this.kite._y+swingY, this.kite._x, this.kite._y);
}
Makes use of perlin noise to create the displacement. Idea is to take the red value of the first pixel in the displacement map to change the _x value of the kite, the blue channel value alters the _y value.
Change bViewWind to true at the top and you'll see a bitmap being updated.
You can play with the values in the perlinNoise call (line 36) to see different effects.
Cheers,
Rob
Thanks to both of you!