PDA

View Full Version : help with simple program


jimnoy
10-22-2008, 12:24 PM
hi

im a total newbie to flash and i cant work this out. im sure its very simple though. id prefer to do it in actionscript 2.0 as 3.0 seems way to complicated. also id prefer clear coding for noob debugging (not necessarily the most elegant)

basically i want a program to take user inputs (numerical) from text boxes, store these inputs as variables, then rotate a symbol according to a function of time using these variables.

so far ive got an input text box called "textbox"
a movie clip symbol called "arm"
a button called "go"

so on opening, the arm should be static.
then user inputs a number into the text box.
then user presses the go button. the text box number should be stored as a variable w, and the arm should start rotating as per f=w*cos(t) (this is not actually the function im using, because my one is really messy)

im guessing the code should be something along the lines of

on (release) {
var w = textbox.text;
}
attached to the button "go"

and

var t=0
go.onRelease = function {
arm._rotation = w*cos(t)
t += 1/12;
}

this is obviously wrong, but you see what im driving at (hopefully). can anyone help me put it all together. should i be using more than one frame? i guess i should probably incorporate some kinda reset button too.

cheers in advance :)

Charlie IronGleet
11-04-2008, 09:32 PM
you need to set up that code you got there in an EnterFrame event loop. Dunno how that works in AS2. but in 3.0 you add an event listener to the stage or movieclip or whatever and listen for an enter frame event. The listener then calls a function every time the screen refreshes (i.e. a frame updates). This will generate your animation.

Charlie IronGleet
11-04-2008, 09:34 PM
oh yeah... and also you'll need to use your equation to figure out the increments by which you want to animate with each enter frame and set up something like xyz._x += whatever increment your equation solved for....

thats the general idea

alile
11-04-2008, 10:03 PM
change this:

on (release) {
var w = textbox.text;
}
attached to the button "go"

and

var t=0
go.onRelease = function {
arm._rotation = w*cos(t)
t += 1/12;
}



to this (or something along these lines, assuming all items have the same parent):

var w:Number = 0;
var t:Number = 0;
var tMax:Number = 10;

go.onRelease = function():Void
{
w = parseFloat(textbox.text);
t = 0;
arm.onEnterFrame = rotateArm;
}

rotateArm = function():Void
{
arm._rotation = w * Math.cos(t);
t += 1/12;
// check completion (if want to ever end)
if(t >= tMax)
{
delete arm.onEnterFrame;
}
}



note that the on(release) is gone. even tho this is the newbie section, in no way do you ever want to use the on(something) syntax. that is a tip, not a condemnation :) hth

Skwinch
11-05-2008, 02:00 AM
The above code is extremely complex, and although it gets the job done and is very well formed, there is at least one much simpler way to do exactly the same thing in what you called "clear code for noob debugging".

It seems very odd to me that you would pick the particular equation that you did, because it will, of course, make the arm wobble back and forth in a sine wave (well, actually cosine wave) pattern, not make the arm rotate at the speed of [whatever you inputted] degrees per frame. But anyway, here's some code using the f=w*cos(t) formula:

First for the frame...

//this goes on the frame
var t = 0
onEnterFrame = function(){
arm._rotation = w*Math.cos(t)
t+=1/12
}and then for the button:

//this goes on the "go" button
on(release){
w = parseFloat(textbox.text)
}If you were, in fact, trying to make the arm go around at [whatever you inputted] degrees per frame, here's an even simpler replacement for the frame code:

//This goes on the frame
onEnterFrame = function(){
arm._rotation += w
}Both of these are workable solutions... I hope one of them is your answer.