Creative COW SIGN IN :: SPONSORS :: ADVERTISING :: ABOUT US :: CONTACT US
Creative COW's LinkedIn GroupCreative COW's Facebook PageCreative COW on TwitterCreative COW's Google+ PageCreative COW on YouTube
ADOBE FLASH:HomeFlash ForumFlash TutorialsFlash Video TutorialsWeb Streaming ForumAdobe FlashPodcast

Re: mouseX problems Actionscript 3.0

COW Forums : Adobe Flash

FAQ   •   VIEW ALL   •   ADD A NEW POST   •   PRINT
Share on Facebook
Respond to this post   •   Return to posts index   •   Read entire thread


Brodd NessetRe: mouseX problems Actionscript 3.0
by on Jun 20, 2011 at 1:20:42 pm

Hi! You already have the basic logic down, in what we can call 'pseudocode'. I put a fully working example below, where you can see the full logic you need displayed.
For this example to work, all you need is a MovieClip placed on Stage with the instance name 'mc_square'. Observe! When you create the MovieClip, you should move it contents so that the 'cross' is at its centre, not at its default upper left corner.

This example could have been made shorter by omitting most of the variables; but it's much tidier with them!

To reduce the script further, vertically, some of the if-sentences could have been combinded - but this would have inflated it horizontally, and made it a lot harder to read.

import flash.events.Event;

stop();

//These must be set:
var BaseAlpha:Number = 0.1;
var RiseAlpha:Number = 0.5;
var FullAlpha:Number = 1;
var BorderSize:Number = 100;

//These are calculated:
var mcSize:Number = mc_square.width / 2;
var LeftEdge:Number = mc_square.x - mcSize;
var RightEdge:Number = mc_square.x + mcSize;
var LeftBorder:Number = LeftEdge - BorderSize;
var RightBorder:Number = RightEdge + BorderSize;

//Initialize:
mc_square.alpha = BaseAlpha;

addEventListener(Event.ENTER_FRAME, checkMousePos);

function checkMousePos(e:Event):void {
if (mouseX < LeftBorder) {
mc_square.alpha = BaseAlpha;
}
if (mouseX > RightBorder) {
mc_square.alpha = BaseAlpha;
}
if (mouseX > LeftBorder && mouseX < LeftEdge) {
mc_square.alpha = RiseAlpha;
}
if (mouseX > RightEdge && mouseX < RightBorder) {
mc_square.alpha = RiseAlpha;
}
if (mouseX > LeftEdge && mouseX < RightEdge) {
mc_square.alpha = FullAlpha;
}
}


To add the same functionality for the Y axis, you can actually just duplicate the whole thing, but alter the variable names accordingly. There are more elegant methods for the whole shebang, but these are also more advanced.



Not everything that can be counted counts, and not everything that counts can be counted.



Posts IndexRead Thread
Reply   Like  
Share on Facebook


Current Message Thread:




LOGIN TO REPLY



FORUMSTUTORIALSFEATURESVIDEOSPODCASTSEVENTSSERVICESNEWSLETTERNEWSBLOGS

Creative COW LinkedIn Group Creative COW Facebook Page Creative COW on Twitter
© 2013 CreativeCOW.net All rights are reserved. - Privacy Policy

[Top]