Copy link to clipboard
Copied
Hey Guys,
Ok i have been looking at a racing game and have used the tutorial from this link here:
http://www.emanueleferonato.com/2007/05/15/create-a-flash-racing-game-tutorial/
What i am having trouble with is trying to create a scrolling background whilst keeping the car static in the centre of the screen. Ane example of what im trying to do is here: http://www.mindmafya.com/index/maps
So i have tried incorporating the code from that scrolling background example, into the car racing game itself but i am having trouble doing so. Is there any way that the code used from the scrolling background can be input into the racing game example to create this scrolling background effect?
Thanks Guys.
Below is the Actions for the Racing Game itself. Check out the whole Racing Game tutorial using the link at the top.
function step(who) { //check to see if the car in question is controlled by the player or by the computer if (_root["car"+who].code == "player") { //we will constantly decrease speed by multiplying it with a number below 1 if (this["speed"+who]>0.3) { this["speed"+who] *= _root.speedDecay; } else { this["speed"+who] = 0; } //the car will react to certain keys //accelerate if (Key.isDown(Key.UP) && this["speed"+who]<_root.maxSpeed) { this["speed"+who] += _root.acceleration; } //brake (reverse) if (Key.isDown(Key.DOWN)) { this["speed"+who] -= _root.backSpeed; } //steer left if (Key.isDown(Key.LEFT) && Math.abs(this["speed"+who])>0.3) { _root["car"+who]._rotation -= _root.rotationStep*(this["speed"+who]/_root.maxSpeed); } //steer right if (Key.isDown(Key.RIGHT) && Math.abs(this["speed"+who])>0.3) { _root["car"+who]._rotation += _root.rotationStep*(this["speed"+who]/_root.maxSpeed); } this["rotation"+who] = _root["car"+who]._rotation; //we calculate the two components of speed (X axis and Y axis) this["speedx"+who] = Math.sin(this["rotation"+who]*(Math.PI/180))*this["speed"+who]; this["speedy"+who] = Math.cos(this["rotation"+who]*(Math.PI/180))*this["speed"+who]*-1; //apply the components on the actual position of the car _root["car"+who]._x += this["speedx"+who]; _root["car"+who]._y += this["speedy"+who]; //the collisions //define the four collision points _root["car"+who].pointLeft = {x:-20, y:0}; _root["car"+who].localToGlobal(_root["car"+who].pointLeft); _root["car"+who].pointRight = {x:20, y:0}; _root["car"+who].localToGlobal(_root["car"+who].pointRight); _root["car"+who].pointFront = {x:0, y:-25}; _root["car"+who].localToGlobal(_root["car"+who].pointFront); _root["car"+who].pointBack = {x:0, y:25}; _root["car"+who].localToGlobal(_root["car"+who].pointBack); //let's use some shorter variable names :) this["lpx"+who] = _root["car"+who].pointLeft.x; this["lpy"+who] = _root["car"+who].pointLeft.y; this["rpx"+who] = _root["car"+who].pointRight.x; this["rpy"+who] = _root["car"+who].pointRight.y; this["fpx"+who] = _root["car"+who].pointFront.x; this["fpy"+who] = _root["car"+who].pointFront.y; this["bpx"+who] = _root["car"+who].pointBack.x; this["bpy"+who] = _root["car"+who].pointBack.y; //check for collisions if (_root.terrain.hitTest(this["lpx"+who], this["lpy"+who], true)) { _root["car"+who]._rotation += 5; this["speed"+who] *= 0.85; } if (_root.terrain.hitTest(this["rpx"+who], this["rpy"+who], true)) { _root["car"+who]._rotation -= 5; this["speed"+who] *= 0.85; } if (_root.terrain.hitTest(this["fpx"+who], this["fpy"+who], true)) { this["speed"+who] = -1; } if (_root.terrain.hitTest(this["bpx"+who], this["bpy"+who], true)) { this["speed"+who] = 1; } //position the shadow of the car _root["shadow"+who]._x = _root["car"+who]._x-4; _root["shadow"+who]._y = _root["car"+who]._y+2; _root["shadow"+who]._rotation = _root["car"+who]._rotation; //checkpoints if (_root["car"+who].hitTest(_root["checkpoint"+_root["currentCheckpoint"+who]])) { //if the current checkpoint is the start line - increase the lap number if (_root["currentCheckpoint"+who] == 1) { if (_root["currentLap"+who] != 0) { _root.setBestLap(); } if (_root["currentLap"+who] == _root.totalLaps){ _root.gotoAndStop("finish"); }else{ _root["currentLap"+who]++; } _root.currentLapTXT = _root["currentLap"+who]+"/10"; } _root["currentCheckpoint"+who]++; //if the current checkpoint is the last checkpoint - set the next checkpoint to the start line if (_root["currentCheckpoint"+who]>_root.checkpoints) { _root["currentCheckpoint"+who] = 1; } } } if (_root["car"+who].code == "computer") { } } function setTimes() { timeElapsed = getTimer()-_root.initialTime; milliseconds = timeElapsed; seconds = Math.floor(milliseconds/1000); minutes = Math.floor(seconds/60); minutesTXT = minutes; secondsTXT = seconds-minutes*60; tensTXT = Math.round((milliseconds-seconds*1000)/10); if (minutesTXT<10) { minutesTXT = "0"+minutesTXT; } if (secondsTXT<10) { secondsTXT = "0"+secondsTXT; } if (tensTXT<10) { tensTXT = "0"+tensTXT; } _root.totalTimeTXT = minutesTXT+"."+secondsTXT+"."+tensTXT; } function setBestLap() { bestTime = getTimer()-_root.lapTime; milliseconds = bestTime; if (oldMilliseconds>milliseconds || oldMilliseconds==null) { oldMilliseconds = milliseconds; seconds = Math.floor(milliseconds/1000); minutes = Math.floor(seconds/60); minutesTXT = minutes; secondsTXT = seconds-minutes*60; tensTXT = Math.round((milliseconds-seconds*1000)/10); if (minutesTXT<10) { minutesTXT = "0"+minutesTXT; } if (secondsTXT<10) { secondsTXT = "0"+secondsTXT; } if (tensTXT<10) { tensTXT = "0"+tensTXT; } _root.bestLapTXT = minutesTXT+"."+secondsTXT+"."+tensTXT; } _root.lapTime = getTimer(); }
Copy link to clipboard
Copied
(did not read) your code. But what you need to do is add something to the function where you controll your car. So make the background a movieclip, and then move the background in a negative stage to the car. So if the car is moving forwards the background move's backwards, and so on for the rest of the directions.
here is a tutorial:
http://active.tutsplus.com/tutorials/games/create-a-racing-game-without-a-3d-engine/
You might not understand it at forst but just keep reading through it, you hace to add controll functions to tutorial though.
Copy link to clipboard
Copied
Thanks for the reply. Is there anyway way i can use the code from http://www.mindmafya.com/index/maps
if(car_forward==true) {
car.rotation += car.RWheel.rotation;
map.y += -speed*Math.sin((car.rotation-90)*Math.PI/180);
map.x += -speed*Math.cos((car.rotation-90)*Math.PI/180);
}
if(car_backward==true) {
car.rotation += -car.RWheel.rotation;
map.y += speed*Math.sin((car.rotation-90)*Math.PI/180);
map.x += speed*Math.cos((car.rotation-90)*Math.PI/180);
}
And insert this into the actions amongst the code from the racing tutorial to create that effect?, or would i have to create more code around this?
Cheers
Copy link to clipboard
Copied
yeah i guess if you replaced that code with the function that moves the car in your code above (again i have not read it)
then yeah maybe, im unsure. Looking at that code briefly looks like its updating a map? not the track...
Copy link to clipboard
Copied
ok just looked at link, yeah should work if you put it in the right place.
Copy link to clipboard
Copied
Ok, ive tried multiple times but it hasnt worked. Im not sure if i have put it in the right place or not though. Any suggestions?
I had also created a seperate layer (map) which the car can drive on using this scrolling background etc.
Copy link to clipboard
Copied
google this ebook: ActionScript 3.0 Game Programming.pdf, it has a great section specifically for that type of game, where the map scrolls and picking up power ups ect. It also has a 3D version if you want your game to be better. Look it up it will help you. Your problem is, is that your new to as3? and your trying to use document classes.. they can be very complicated if not familiar with as3. I suggest you stick to learning timline as3 untill you are confident.
Copy link to clipboard
Copied
Although math concepts are the same, this is an AS3 forum and you ask about AS2 code. You should post it on AS2 forum.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more