PDA

View Full Version : needle movement script?



spence
June 21st, 2004, 07:19 AM
Hi,

Im currently trying to create a type of speedometer thing which reacts via various buttons on the screen.

I done a simple script for the needle to move so many degrees left or right but this simple script doesn't stop the needle from continuing to go to far left or right (min/max on dial). Is there a way of stoping the needle once it has reached say 10 degrees and 120 degrees?

My script is:

bad_btn.onPress = function (){
needle_mc._rotation +=16;
}
good_btn.onPress = function (){
needle_mc._rotation -=16;
}

bad and good buttons determine the left and right

Im also interested in whether once the needle has reached it max point this would trigger of another function etc

Thanks

Spence

tommythewolfboy
June 21st, 2004, 08:01 AM
bad_btn.onPress = function() {
// only allow button press if rotation is less than 120
if (needle_mc._rotation<120) {
needle_mc._rotation += 16;
mcAng = _root.needle_mc._rotation;
}
};
good_btn.onPress = function() {
// only allow button press if rotation is greater than -120
if (needle_mc._rotation>-120) {
needle_mc._rotation -= 16;
mcAng = _root.needle_mc._rotation;
};
};

tommythewolfboy
June 21st, 2004, 08:02 AM
sorry - not the values you wanted, but you get the idea ... play around with it to get what you want

spence
June 21st, 2004, 08:06 AM
Thanks i'll give it a try....

Any ideas on the trigger point?

Is collision detection an option?

Spence

tommythewolfboy
June 21st, 2004, 09:55 AM
no need for collision detection, you can trigger another function by checking against the value of needle_mc._rotation (you would need to have a movie that did this onEnterFrame event rather than button press), e.g. [I haven't tested this]
needle_mc.onEnterFrame = function () {
if (needle_mc._rotation >=-100) {
_root.clipA.gotoAndPlay(1);
}
else if (needle_mc._rotation>100) {
_root.clipB.gotoAndPlay(1);
}
};
This would play different clips in the stage, depending upon the value of needle_mc._rotation. You can obviously do anything as part of this function, i've just specified gotoAndPlay for simplicities sake ...

spence
June 21st, 2004, 10:56 AM
Thanks again :} ..... I'll try the above stuff too.

Spence :beam:

tommythewolfboy
June 21st, 2004, 11:17 AM
No problem. Let's just hope it works ;¬) Shout if not!

Prophet
June 21st, 2004, 01:39 PM
so u want sumit similar to this?
http://www.freewebs.com/pweb/celicapreload.swf
(i have script in to change the main content as well but the content is only half done so ive not associated it...)
(also bear in mind this was only an excersie so dont laff at the design/layout!!)
(also i have sounds on there as well but there are issues with that atm so theyr not ther eivr!! lol)

i can give you code direct from the different bits ifu want...

Prophet.

spence
June 22nd, 2004, 04:46 AM
Yeah the code for the accelerate button/speedo would be great....my project requires tick boxes being ticked/unticked and the dial reacting to these ticks etc

Thanks

Spence

Prophet
June 22nd, 2004, 01:21 PM
the code that goes on the pedal is this:
on(press){
_parent.Frevup()
_root.pedalDown = true
_root.needleUp = true
_root.needleDown = false
}
on(release){
_parent.Frevdown()
_root.pedalDown = false
_root.needleUp = false
_root.needleDown = true
_parent.wergo()
}
on (releaseOutside) {
_parent.Frevdown()
_root.pedalDown = false
_root.needleUp = false
_root.needleDown = true
_parent.wergo()
}

the code on the parent frame is this:
//sounds:
revUp1=new Sound(this);
revUp1.attachSound("revup")
revMax=new Sound(this);
revMax.attachSound("revupB")
revDown = new Sound(this)
revDown.attachSound("revdown")
backingtrack = new Sound(this)
backingtrack.attachSound("backtrak")
backingtrack.start(0,10)
startup = 0
if(playing == undefined){
playing = false
}
function Frevup(){
if(playing == 3){
startupPC = ((revDown.position/1000)/3.05)*100 //use basic percentage equations to work out how far into the second sound to start playing as the rev up and rev down sounds are different lengths due to the quicker recession of the needle on the way down
startUpPC1 = 100-startupPC
startUp = (startupPC1/100)*4.16
}
else if(playing == 0){
startup = 0
}
revDown.stop("revdown")
revUp1.stop("revup")
revMax.stop("revupB")
revUp1.start(startup, 1)
playing = 1
revUp1.onSoundComplete = function(){
revMax.start(0.5,20)
playing = 2
}
}
function Frevdown(){
revDown.stop("revdown")
revUp1.stop("revup")
revMax.stop("revupB")
if(playing == 1){
startdwnPC = ((revUp1.position/1000)/4.16)*100
startdwnPC1 = 100-startdwnPC
startdwn = (startdwnPC1/100)*3.05
}
else if(playing == 2){
startdwn = 0
}
revDown.start(startdwn, 1)
playing = 3
revDown.onSoundComplete = function(){
playing = 0
}
}
//navigation function:
function wergo(){
if(_root.wergo == 1){
home.swapDepths(-1500)
home.wipe.gotoAndPlay(2)
}
if(_root.wergo == 2){
pics.preloader.gallerypreload = true
pics.swapDepths(-1500)
pics.wipe.gotoAndPlay(2)
}
if(_root.wergo == 3){
stats.swapDepths(-1500)
stats.wipe.gotoAndPlay(2)
}
} note that FrevUp and Frevdown are only to do with sounds so u can take those out if u want (or keep it in - it makes no diff2me!!)- i only included them coz ther are links to it on the pedal code...
the code that deals with the needle is basically this:
if(maxRev == true){
if( dir == "down"){
_parent.needle._rotation = _parent.needle._rotation-9
}
else if( dir == "up"){
_parent.needle._rotation =_parent.needle._rotation+6.5
}
if(_parent.needle._rotation >= 154){
dir = "down"
}
else if(_parent.needle._rotation <= 150){
dir = "up"
}

}

if(_root.needleUp == true){
_parent.needle._rotation = _parent.needle._rotation+1*_root.speed
}
if(_root.needleDown == true){
_parent.needle._rotation = _parent.needle._rotation-1.5*_root.speed
maxRev = false
}
if(_parent.needle._rotation <= -60){
if(_root.needleDown == true){
_root.needleDown = false
_parent.needle._rotation = -60
}
}
if(_parent.needle._rotation >= 155){
if(_root.needleUp == true){
_root.needleUp = false
maxRev = true
}
} and there is also a bunch of if statements that increase the spped of easing in relation to how far it is rotated...
also note that the start rotation was set to -60 to avoid problems increasing rotation past 180...

sorry if theres a lot of unwanted code there but you asked for the code on the button and it wasnt very helpful and i interlink all my code so iguess its a mess to steal from! ;) lol

enjoy!

Prophet.

PS send me a link of your finished product ifu dont mind?

spence
June 22nd, 2004, 02:23 PM
Thanks...:beam:

I'll get cracking on my project now Im armed with all this info.

I'll keep you updated how it goes.

Thanks again.. :pleased:

Spence

Prophet
June 22nd, 2004, 06:10 PM
sok just make sure u do sumit i dint do wontcha - finish it! lol
hav fun! ;)

Prophet.

tommythewolfboy
June 23rd, 2004, 06:17 AM
Nice stuff prophet :¬)

I'd be interested in the easing statements if you were prepared to share that too?

t

Prophet
June 23rd, 2004, 01:39 PM
its simple really... rather than have the _x or _y values changing, you just substitute in _rotation values... of course, like i said, you need to define your start value as a negative value if you are intending to go past 180 and ive tried and you just cant go past 360 without it doing a quick reverse spin when you reach the threshold between starting an ending... at least i cant get rid of that anyway... maybe you can, if u can, lemme know!!
and of course if you want to have a similar speed increase thing as on my dummy site above, you just have an onClipEvent(enterFrame) checking for the rotaion values in infamous if statements and change the speed accordingly ;)
hmmm.... i dont think theres a scrap of code left in that file to share anymore!! oh... wait, heres some stop() codes you can have as well! :P lol

happy flashing all!

Prophet.

tommythewolfboy
June 24th, 2004, 03:47 AM
LOL.
Thanks

Prophet
June 24th, 2004, 01:20 PM
no prob:D

Prophet.