Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Race game using action script 1

Status
Not open for further replies.

Greenleaf

Programmer
Feb 14, 2002
93
IT
Hello, I am trying to develop a simple race game, using almost only Action script. I am in a little bit of trouble because I haven't a lot of experience with this language.
One of the problems is:
I need some trees a the border of the road; I'm doing like this:

Code:
duration=1000;
function createtree():Void {
	if (i<20) {
		attachMovie("tree", "tree"+i, i);
		this["tree"+i].dir = Math.floor(Math.random()*2);
		i++;
	}
}
intervalId = setInterval(this, "createtree", duration);

in the movie clip of the tree I write code to set the position of the trees and how they have to move; when they exit the stage, I make them re-enter at the starting position. (There is a condition test to determine if the tree is a "right" or a "left" one):

Code:
this._xscale = 10;
this._yscale = 10;
this._y = 190;
if (this.dir == 0) {
			this._x = 220;
		} else {
			this._x = 380;
		}
	
function movetree(vel) {
	if (this._y>Stage.height) {
		this._y = 190;
		this._xscale = this._yscale=10;
		if (this.dir == 0) {
			this._x = 220;
		} else {
			this._x = 380;
		}
	}
	if (this.dir == 0) {
		this._x -= vel*(this._y/190);
	} else {
		this._x += vel*(this._y/190);
	}
	this._y += vel;
	this._xscale = this._yscale=(this._y-190)+10;
}
from outside I call:

Code:
onClipEvent (enterFrame) {
	if (Key.isDown(Key.UP)) {
		vy += ay;
	}
	vy *= f;
	for (j=1; j<10; j++) {
		this["tree"+j].movetree(vy);
	}
}
The problem is that at first the trees move correctly, but then their number decreases until there are no trees left. I cannot understand what's wrong.
I would be very grateful if someone could give me a hint.
 
In your code I can't see where you defined the variables i, vy, ay, and f; but if you defined those it'll work fine. (Although the code can be written neater!)

Also,

[tt]for (j=1; j<10; j++)[/tt]

shouldn't it be:

[tt]for (j=1; j<20; j++)[/tt]

?

Kenneth Kawamoto
 
Yes, you're right, it should be 20 and 20 in both places; I fixed it but nothing changes. Yes, I omitted to copy here the definition of the variables but I have defined them. (Tell me how should be written a better code; i need to learn).
Anyway I found out that the trees disappear after a while when I define the cars that come towards the player. I wrote a similar code to generate random cars and move them and only when I create these cars the trees slowly disappear, but without the car the trees go on and on. What kind of conflict could it be here?
 
Testing with the smallest code possibile I create two functions (almost identical), one for trees and one for cars. Whenever an object exits the stage it returns back.
If I call the two functions, the trees disappear after a while, the cars go on without problems. If I don't call the car function, the trees go on and on. (However, it's curious that I cannot produce the symmetric effect:trees going on and cars disappear).
What could it be? How can I generate random objects of different types?
 
This is the entire code I put in the movie clip where I attach the movie clips (trees and cars)
Code:
//initial settings//

scontro = 0;
ax = 5;
ay = 4;
at = 3;
f = 0.8;
freno = 0.4;
vy = 0;
vx = 0;
traffic_vel = 0;
i = 1;

//function called when there is a car crash

function bang(scont) {
	traffic_vel = vy;
	if (scont == 1) {
		scontro = 1;
		vy = 0;
	} else {
		scontro = 0;
	}
}

//functions to generate trees and cars//

duration = 500;
duration2 = 1000;
var intervalId:Number;
var intervalId2:Number;
function creaalberi():Void {
	if (i<20) {
		attachMovie("albero", "albero"+i, i);
		this["albero"+i].dir = random(2);
		this["albero"+i]._xscale = 10;
		this["albero"+i]._yscale = 10;
		i++;
	}
}
intervalId = setInterval(this, "creaalberi", duration);
var k:Number = 0;
var traffico:Array = new Array();
function creatraffico() {
	if (random(2) == 0) {
		attachMovie("civic_traffico", "civic"+k, k, {_x:(random(110)+220), _y:190});
		this["civic"+k]._xscale = 10;
		this["civic"+k]._yscale = 10;
		traffico[traffico.length] = k;
		k++;
	}
}
intervalId2 = setInterval(this, "creatraffico", duration2);

//player movement with calls to the functions that make trees and cars move -these functions are in the movie clips of the tree and of the car

onEnterFrame = function () {
	if (Key.isDown(Key.LEFT)) {
		vx -= ax;
	}
	if (Key.isDown(Key.RIGHT)) {
		vx += ax;
	}
	if (Key.isDown(Key.UP)) {
		if (scontro == 0) {
			vy += ay;
			traffic_vel += at;
		} else {
			vy = 0;
		}
	}
	if (Key.isDown(Key.DOWN)) {
		if (vy>0) {
			vy -= freno*ay;
		}
	}
	vx *= f;
	vy *= f;
	traffic_vel *= f;
	_root.giocatore._x = Math.max(_root.giocatore._width/2, _root.giocatore._x+vx);
	_root.giocatore._x = Math.min(Stage.width-_root.giocatore._width/2, _root.giocatore._x+vx);
	for (t=1; t<20; t++) {
		this["albero"+t].muovialberi(vy);
	}
	if (scontro == 0) {
		for (m=0; m<traffico.length; m++) {
			this["civic"+m].muovitraffico(traffic_vel, scontro);
		}
	} else {
		for (m=0; m<traffico.length; m++) {
			this["civic"+m].muovitraffico(2*traffic_vel, scontro);
		}
	}
};
 
Action script in the Movie clip car "civic_traffico"
Code:
this._xscale = this._yscale=10;
function muovitraffico(vel, scont) {
	if (this._y>=190) {
		this._visible = true;
	}
	if (scont == 0) {
		if (this.hit.hitTest(_root.giocatore.hit)) {
			_root.stradaterreno.bang(1);
		} else {
			this._y += vel;
			this._xscale = this._yscale=((this._y-190)+10)/1.6;
		}
	} else {
		this._y -= vel;
		this._xscale = this._yscale=((this._y-190)+10)/1.6;
		if (this._y<=200) {
			
			this._visible = false;
                        removeMovieClip(this);
		        traffico.splice(j, 1);
		}
		setTimeout(_root.stradaterreno.bang, 1000, 0);
	}
	if (this._y>=Stage.height) {
		removeMovieClip(this);
		traffico.splice(j, 1);
	}
}

and this is the movie clip of the tree "albero"

Code:
this._xscale = 10;
this._yscale = 10;
this._y = 190;
if (this.dir == 0) {
			this._x = 220;
		} else {
			this._x = 380;
		}
	
function muovialberi(vel) {
	if (this._y>360) {
		this._y = 190;
		this._xscale = this._yscale=10;
		if (this.dir == 0) {
			this._x = 220;
		} else {
			this._x = 380;
		}
	}
	if (this.dir == 0) {
		this._x -= vel*(this._y/190);
	} else {
		this._x += vel*(this._y/190);
	}
	this._y += vel;
	this._xscale = this._yscale=(this._y-190)+10;
}
after a while the trees disappear one by one.
 
Oh thank you so much, I wouldn't have guessed by myself. You've been very helpful.
I think that I'll need more help later on.
Thank you for now! :)
 
Here I am again, now I'm busy with the "stripes" at both sides of the road. I have a mask level with the shape of the vertical stripes and I have a masked level where I'd like to put horizontal bands. Well, if I manually put the bands on the masked level, when I run the swf I can see that the mask is working fine. If I dinamically attach the horizontal bands on the masked level,when I run the swf, the bands are above everything and the mask does not mask. How could I proceed?
 
I cannot make it work.
My mask level has a mc called "maskera";
the masked level is called "avanzamento";
I'm trying to attach the mc "traccia" in "avanzamento";
if in avanzamento I write
Code:
setMask(_root.maskera)
the mask masks not only the level avanzamento, it masks the other levels as well.
If I try with:
Code:
 attachMovie("traccia", "traccia"+i, i);
 this["traccia"+i].setMask(_root.maskera);
I get no result. :-(
 
1. As I mentioned before layers (you are talking about timeline layers, not levels) have no meanings - they are visual guide only, there are no layers in SWF and you cannot target them in script.

2. You cannot use one MovieClip for more than one mask. If you use [tt]_root.maskera[/tt] to mask [tt]traccia0[/tt], [tt]_root.maskera[/tt] cannot be used to mask [tt]traccia1[/tt] - if you do [tt]traccia0[/tt] will lose the mask.

Kenneth Kawamoto
 
Hello, I'm back again with the same game but new question.
I manage everything by code except for road curves. I did a form interpolation but the problem is that once the interpolation starts, it doesnt' stop until the last frame. I would like to cohordinate this interpolation with the speed of the car; is there a way to control the speed of the interpolation according to the variable describing the speed of the car?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top