PDA

View Full Version : Funny problem but still a problem...


iMantz
01-04-2011, 03:26 AM
Hey everyone... I cant call myself as a beginner in making Action scripts, I am real newbie here and trying to practise. So I was doing flying object tutorial. To follow tutorial is easy, you just make the frames, shapes it asks and copy action script from tutorial. It was more challenging to understand the script. My first script for flying hearts (those objects were in tutorial lol) was taken from tutorial and it looked like this:

flashdesignerzone.com/tutorials/t1059.swf
maxhearts = 100;

var hearts = new Array();

for(i=0;i<maxhearts;i++)
{
hearts[i] = heart.duplicateMovieClip("heart"+i,100+i);

// put it in random place
hearts[i]._x = Stage.width*Math.random();
hearts[i]._y = Stage.height*Math.random();
hearts[i]._xscale = 40+Math.random()*60;
hearts[i]._yscale = hearts[i]._xscale;

hearts[i].yspeed = Math.random()*4+ 1;
hearts[i].increment = -0.025+Math.random()*0.05;
hearts[i].onEnterFrame = function() {
this.radians = this.increment + this.radians;
this._y = this._y - this.yspeed;
this._x = Math.sin(this.radians) + this._x;
if (this._y<-20) {
this._y = Stage.height;
this._x = 0-10+Math.random()*Stage.width;
}
}
}

In the example was shown how different sizes hearts are flying up, some of them were flying to different side directions (Moving on X-axis). However, I could not get the same result, my objects were flying only upwards without moving to sides. I couldn't understand why my result was different as I used same script. I wasn't happy with my result since I wanted to learn so I started searching how to make objects moving to different sides. I found a script where object is moving in circle and decided to use this code:

onClipEvent (load) {
centerY = _y;
centerX = _x;
xRadius = 100;
yRadius = 50;
rotationIncrement = 6;
currentRotation = 0;
}
onClipEvent (enterFrame) {
currentRotation += rotationIncrement;
currentRotation %= 360;
var radians = Math.PI/180*currentRotation;
var y = Math.sin (radians);
var x = Math.cos (radians);
_y = centerY + y*yRadius;
_x = centerX + x*xRadius;
}

After a few hours of work (I had to analyse taken scripts, to find out meanings of some codes), it was really challenging as it was my first time using flash maker and I finally got a result I wanted. It differs from the first example as there each object is flying to different sides. My made flash looks likes this:

imantz.weebly.com/uploads/4/8/5/6/4856463/bubbles.swf

Before telling about the problem I got I want to share...
that while I was preparing this post I noticed... I found that there was a mistake in the first code, somebody who made that tutorial gave wrong script. It seems the spent hours on analysing scripts wasnt meaningless as it is good to find mistakes =]
The first script would work the way it should work if you type:
this.radians = 0; before the function line =]

PROBLEM

OK, so now about the problem I got... I was trying to improve my final result with bubbles which u could see in the given link. What did I want to do is..... I tried to make bubbles go up from different corners to different sides while moving about their center in x-axis. What I mean.. bubbles from left corner go upwards or diagonally to right + they are slightly moving left-right AND bubbles form right corner go upwards or diagonally to left + slight left-right movement.

I used the same code I got from combining previous scripts. Basically, I used 2 similar scripts - one for left, another for right sides.

//script for LEFT corner
maxballsL = 50;

var ballsL = new Array();

for(i=0;i<maxballsL;i++)
{
ballsL[i] = ballL.duplicateMovieClip("ballL"+i,100+i);

// put it in random place
ballsL[i]._x = 50*Math.random()+10*Math.random();
ballsL[i]._y = 500 - 50*Math.random();
ballsL[i].centerY = this._y;
ballsL[i].centerX = this._x;

ballsL[i].xRadius = 40;
ballsL[i].yRadius = 1;
ballsL[i].rotationIncrement = 2+ Math.random()*10;
ballsL[i].currentRotation = 360;

ballsL[i]._xscale = 40 + 60*Math.random();
ballsL[i]._yscale = ballsL[i]._xscale;

ballsL[i].yspeed = 4*Math.random() + 1;
ballsL[i].xspeed = 5*Math.random();
ballsL[i].increment = -0.025+Math.random()*0.05;

ballsL[i].onEnterFrame = function() {

this.currentRotation += this.rotationIncrement + 2 + Math.random()*2;

this.radians = Math.PI/180*this.currentRotation;
_y = Math.sin (this.radians);
_x = Math.cos (this.radians);


this.radians = this.increment + this.radians;
this._y = this._y - this.yspeed;
this._x = Math.sin(this.radians) + this._x + this.xspeed;
_y = this.centerY + y*this.yRadius + 3*Math.random();
_x = this.centerX + x*this.xRadius + 60*Math.random();
if (this._y<-20) {
this._y = Stage.height;
this._x = 50*Math.random();
}
}
}

//script for RIGHT corner

maxballsR = 50;

var ballsR = new Array();

for(i=0;i<maxballsR;i++)
{
ballsR[i] = ballR.duplicateMovieClip("ballR"+i,100+i);

// put it in random place
ballsR[i]._x = 600 - 50*Math.random() - 10*Math.random();
ballsR[i]._y = 500 - 50*Math.random();
ballsR[i].centerY = this._y;
ballsR[i].centerX = this._x;

ballsR[i].xRadius = 40;
ballsR[i].yRadius = 1;
ballsR[i].rotationIncrement = 2+ Math.random()*10;
ballsR[i].currentRotation = 360;

ballsR[i]._xscale = 40 + 60*Math.random();
ballsR[i]._yscale = ballsR[i]._xscale;

ballsR[i].yspeed = 4*Math.random() + 1;
ballsR[i].xspeed = -5*Math.random();
ballsR[i].increment = -0.025+Math.random()*0.05;

ballsR[i].onEnterFrame = function() {

this.currentRotation += this.rotationIncrement + 2 + Math.random()*2;

this.radians = Math.PI/180*this.currentRotation;
_y = Math.sin (this.radians);
_x = Math.cos (this.radians);


this.radians = this.increment + this.radians;
this._y = this._y - this.yspeed;
this._x = Math.sin(this.radians) + this._x + this.xspeed;
_y = this.centerY + y*this.yRadius + 3*Math.random();
_x = this.centerX + x*this.xRadius + 60*Math.random();
if (this._y<-20) {
this._y = Stage.height;
this._x = 600 - 50*Math.random();
}
}
}

What I get? Example:
If I put ...
maxballsL = 50 maxballsR = 50 ... only bubbles from the right appears
maxballsL=50 maxballsR = 0 .... bubbles from left appears
maxballsL=1 maxballsR=1 ... 1 bubble from right appears
maxballsL=3 maxballsR=2 ... 1 bubble from left and 2 bubbles from right appears

Could anyone tell me whats wrong here?

I believe my script is too long and it is possible to make it shorter for the same result... feel free to share other opinions not only related to problem

THANKS

Also sorry for such a long post, it seems I was too much excited about making scripts xD