Happy New Year everyone,
I'm trying bits of code to come to terms with AS3 within KM7. Eventually this will be used for students to move the arms of a clock to the correct time. Later in another project I want to move one arm to a specific angle for maths.
The problem is the mc rotates from the middle not the end and it needs to also go backwards.
Any help appreciated.
var mouseposx:int;
var mouseposy:int;
var dragablemin = false; // start with it still
var dragablehour = false;
function mouseClickHandler(evt:MouseEvent):void{ //test to see if it can be moved
if(dragablemin == false){
dragablemin = true;
} else if(dragablemin == true){
dragablemin = false;
}
trace(dragablemin);
}
function mouseMoveHandler(evt:MouseEvent):void{ // this shows rotation forward but from centre of mc not the end
if(dragablemin == true){
minutehand.rotation += 3; // mouse moves minute hand forward (+= 3) but "swaps' when passing over hour hand. How can this be stopped and
} // (-= 3 moves it backwards so how can we have both - when student makes mistake)
function hourMoveHandler(evt:MouseEvent):void{ // rotates from the centre not the end
if(dragablehour == true){
hourhand.rotation += 3; // same as before you want the student to be able to go backwards if they made a mistake (-= 3)
}
Move the clock hands inside their Movie Clips so the the end is at the center point, Also draw the hands straight UP so that coincides with zero roatation, then as you rotate the angles will be 90 for 3 and 360 for 12, you will have to reset the angle when passing 360 as it will continue to count (i.e. rotation of 360+45 for 3 oclock)
When you click on an object set a variable to indicate a "selected" status or make a set of radio buttons to select which hand to use.
Then track the mouse movement and if moving left go rotation- moving right rotation+
Hi Bret,
Can you explain that to me? I've made a simpler clock using
minutehand.addEventListener(MouseEvent.CLICK, onClick); // tried putting this in a button to move forward but like student clicking on hand.
function onClick(event:MouseEvent):void
{minutehand.rotation += 30; }
but can't have a function that moves it back ( think this is what you mean).
Also would have that this is a great place for the KM slider but can't assign the values here.
Have included the fun if you have the time to look at it.
thanks
ja
Thanks for the reply. The .fun shows both hands move forward now - had the same function for both hands before. Can't have it moving backwards or check to see if the student entered the time correctly, though.
Really would like to understand the slider as well.
ja
There is some information on the slider in the KM docs but I don't know if that would be enough. Look at the docs for ScrollBar class.
If you want one click handler to move both forward and backward, you might want to know that when you handle a click event, you can check if the shift or ctrl key was also pressed. That way you can assign different behaviours to a normal click and a click + shift or a click + ctrl .
Fun is not the word I'd use, right now! Your example is great.
How did you link both arms together like a real clock and later test for the right answer? I see you've also got the arms going forward and backwards.
This is what I used originally before going to the simpler code.
var mouseposx:int;
var mouseposy:int;
var dragablemin = false;
var dragablehour = false;
var minutehand = 0;
var hourhand = 0;
function mouseClickHandler(evt:MouseEvent):void{ //test to see if it can be moved
if(dragablemin == false){
dragablemin = true;
} else if(dragablemin == true){
dragablemin = false;
}
trace(dragablemin);
}
function mouseMoveHandler(evt:MouseEvent):void{
if(dragablemin == true){
minutehand.rotation += 1; // mouse moves minute hand forward (+= 3) but "swaps' when passing over hour hand. How can this be stopped and
} // (-= 3 moves it backwards so how can we have both - when student makes mistake)
{hourhand.rotation +=3;} // moves hour hand
}
wow, light years beyond me. The only way for me to learn is take simple code and try to use it. Is there any way my .fun can used to achieve a simpler version? I'm still not sure how to trigger the next question once the student has got the right time.
Thanks for the advice but way above me!
ja
Code written by someone else almost always looks more complicated.
I'll explain what I did.
When the user clicks one of the dots on the arms of the clock, that arm is set as moving for as long as the user keeps pressing. When the mouse is moved while one of the arms is indicated as moving, the mouse coordinate is compared with the coordinates of the center of the clock and the arm is set according to the angle those two coordinates make.
Thanks for the explanation. I'll sift through this - still too much for me! Once the point (time on clock face) was correct, (when the hands glow)how could you add a score (up to 5) and a question counter each time you reset (allow 5 resets before a score is given?)
As I've said before KM is frustrating because it makes you want to do more than you are capable of!!!! At least it stops the brain cells switching off!
All of the helper's time here appreciated.
ja
@Jarnold,
To keep track of score, you can define a variable outside of a function. That way it can be accessed by all functions. If the reset button is pressed, you could increase the total number so you know when it reaches five. If the time is set right, you can increase a counter for the amount of successes.
But I guess you would need a check button instead because at the moment you can simply turn the arms as much as you want until you reach the right time. It might make more sense to not show anything when it is right and make the arms show a red or green glow when a check button is pressed so there's no way to cheat.
Hi again I can't get over how sophisticated this is instead of using a jpg for the clock face. Would I have to do something like this to have the hour numbers? Not my script.
var txt_TextField_12:TextField = new TextField();
A few remarks :
- You can declare the variables as int since they have no decimals.
- Just ++ is enough for an increase.
- To compare values, use ==, not =.
var score:int = 0;
var counter:int = 0;
score++;
counter++;
if (score == counter){
// action
}
Maybe it would also be required to lock the arms of the clock once the correct time has been reached to prevent recounting the same task multiple times.