-
Clouds/stars moving horizontally help?
So I have this great code that works for creating stars/clouds/moving objects randomly across the stage, but I cant get it to change direction to horizontally rather vertically. Help? Heres the code, but I think that a simple change will get the results I want.:
var stars = 40;
var maxSpeed = 16;
var minSpeed = 2;
for (var i = 0; i<stars; i++) {
var mc = this.attachMovie("star", "star"+i, i);
mc._x = random(Stage.width);
mc._y = random(Stage.height);
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
var size = random(2)+0.6*(random(4));
mc._width = size;
mc._height = size;
}
function moveStars() {
for (var j = 0; j<stars; j++) {
var mc = this["star"+j];
if (mc._y<Stage.height) {
mc._y += mc.speed;
} else {
mc._y = 1
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
mc._x = random(Stage.width);
}
}
}
help?
-
Trainee coder
Change the _y propery to _x (as I've done below)
PHP Code:
function moveStars() {
for (var j = 0; j<stars; j++) {
var mc = this["star"+j];
if (mc._y<Stage.height) {
mc._x += mc.speed;
} else {
mc._x = 1
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
mc._x = random(Stage.width);
}
}
}
Viza.
Last edited by Viza; 02-08-2008 at 01:40 AM.
-
Elvis...who tha f**k is Elvis?
Well, the if check has to be based on x values too:
Code:
if (mc._x<Stage._width) {
instead of
Code:
if (mc._y<Stage.height) {
...also change this:
Code:
mc._x = random(Stage.width);
to this:
Code:
mc._y = random(Stage._height);
In flash:
x means moving sideways
y means moving up and down
-
It still doesn't seem to work... In my original code, before you posted, I already changed x to y and it would work for a few seconds, then stop recreating stars. What am I to do?
-
I've tried a few more things, but not getting much progress. Help Plezzz?
-
Hype over content...
Post your new code so we can see what you've changed.
Squize.
-
So basically, the original code I posted works fine, but if I change the x to y thing (obvious thing I know) it works temporarily and suddenly doesn't create any more random stars (bold is changed):
PHP Code:
var stars = 40;
var maxSpeed = 16;
var minSpeed = 2;
for (var i = 0; i<stars; i++) {
var mc = this.attachMovie("star", "star"+i, i);
mc._x = random(Stage.width);
mc._y = random(Stage.height);
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
var size = random(2)+0.6*(random(4));
mc._width = size;
mc._height = size;
}
function moveStars() {
for (var j = 0; j<stars; j++) {
var mc = this["star"+j];
if (mc._y<Stage.height) {
[B]mc._x += mc.speed;[/B]
} else {
mc._y = 1
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
mc._x = random(Stage.width);
}
}
}
So i took what viza and phreax said and adjusted it as well, but it doesn't work period.(I don't think I should post because its up there) I mean there code works, no offense, no errors are made, but it doesn't create any stars (not worthy, not meaning to bash). So, i still am at a dead end. Help?
-
Woops tried to bold the code up there, but it thought it was actionscript. ignore that, and thats what I changed
-
Senior Member
Your moveStars function should look like this:
PHP Code:
function moveStars() {
for (var j = 0; j<stars; j++) {
var mc = this["star"+j];
if (mc._x < Stage.width) {
mc._x += mc.speed;
} else {
mc._x = 1;
mc.speed = random(maxSpeed-minSpeed) + minSpeed;
mc._y = random(Stage.height);
}
}
}
-
Tonypa, it works great, but what if I wanted my stars to go the opposite way. Can you teach me how to distinguish the height and the location of stars so I can make my stars go left right, up, or down?
-
Senior Member
Making the stars move in every direction is possible:
Add direction variable when star is created
PHP Code:
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
mc.direction = 1;
if(random(2) == 1){
mc.direction = -1;
}
Move star by adding direction to speed:
PHP Code:
mc._x += mc.speed * mc.direction;
You also need to check if star has gone out of stage from both ends:
PHP Code:
if (mc._x < Stage.width && mc._x > 0) {
mc._x += mc.speed * mc.direction;
}
And reset the star to other side based on its direction:
PHP Code:
else {
if(mc.direction == 1){
mc._x = 1;
}else{
mc._x = Stage.width;
}
mc.speed = random(maxSpeed-minSpeed) + minSpeed;
mc._y = random(Stage.height);
}
-
Making the stars move in every direction is possible:
I wasn't really asking to move them in all direction, just be able to change a but of code to make them move in the direction I want. What you posted ^^^ probably works just as well. So, how can I use your direction variable to change my direction? Can you break down what variables I change to move them in either north, south, east, or west?
-
Tonypa. I still am confused. How do I move the clouds up rather than down, or right rather than left?
-
Wait where is this script, the frame or the star? And what is the star's instance name?
+ +Chuck Norris = Awesome
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|