View Full Version : Button Object movement?
originaliron
10-12-2007, 07:10 PM
I am having trouble with having an object move when a button is clicked.
When the game user clicks a button that I have on stage a objects is told to move up -= 2 pixels. I have it set up as a mouse event.CLICK
It works but you have to click the button over and over to have the object move. I want to have it set-up so when the button is held down after the click the object will move continuously up. Rather than having to click over and over.
the code look like this.
button_btn.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
object_mc.y -= 2;
}
Any help would be great.
Thanks
stompwampa
10-12-2007, 07:24 PM
First of all, you're using AS 2.0 code where it should be AS 3.0. Also, you should use a MOUSE_DOWN instead of CLICK handler if the button is to be held down.
In order to make the object continuosly move you can do a couple of things:
1. Use a Timer for it so every time the timer trigger, the object moves.
2. Use an ENTER_FRAME event to do it
I'll post some code in little bit...
var myTimer:Timer = new Timer(50);
myTimer.addEventListener(TimerEvent.TIMER, moveTimer);
button_btn.addEventListener(MouseEvent.MOUSE_DOWN, objectMove);
button_btn.addEventListener(MouseEvent.MOUSE_UP, objectStop);
function objectMove(event:MouseEvent):void
{
myTimer.start();
}
function objectStop(event:MouseEvent):void
{
myTimer.stop();
}
function moveTimer(event:TimerEvent):void
{
this.object_mc.y -= 2;
}
Sekhar
10-12-2007, 07:26 PM
What you want to do is: rather than move the object in the click handler, start a timer that triggers regularly and moves the object at each trigger. Here's a quick example:
const MOVE_INTERVAL = 50;
var timer:Timer = new Timer(MOVE_INTERVAL);
timer.addEventListener("timer", moveObject);
function moveObject(evt:TimerEvent) {
object_mc.y -= 2;
}
button.addEventListener("mouseOver", startMove);
function startMove(evt:MouseEvent) {
timer.start();
}
button.addEventListener("mouseOut", stopMove);
function stopMove(evt:MouseEvent) {
timer.stop();
}
Sekhar
10-12-2007, 07:36 PM
In order to make the object continuosly move you can do a couple of things:
Oops, sorry Stomp; I must've posted concurrently - didn't mean to cut you off.
originaliron
10-12-2007, 07:55 PM
This is what I have. I am very new to ActionScript. Thanks for the posts. What am I missing in my code?
const MOVE_INTERVAL = 200;
var timer:Timer = new Timer(MOVE_INTERVAL);
timer.addEventListener(Event.Timer, moveObject);
up_mc.addEventListener(MouseEvent.MOUSE_DOWN, up_down);
up_mc.addEventListener(MouseEvent.MOUSE_UP, up_up);
up_mc.addEventListener(MouseEvent.ROLL_OVER, up_over);
up_mc.addEventListener(MouseEvent.ROLL_OUT, up_out);
function timer(event:TimerEvent):void
{
balloon_mc.y -= 2;
}
function up_over(event:MouseEvent):void
{
event.target.scaleX = 1.2;
event.target.scaleY = 1.2;
event.target.alpha = .9;
}
function up_out(event:MouseEvent):void
{
event.target.scaleX = 1;
event.target.scaleY = 1;
event.target.alpha = 1;
}
function up_down(event:MouseEvent):void
{
timer.start();
}
function up_up(event:MouseEvent):void
{
timer.stop();
}
up_mc.buttonMode = true;
stompwampa
10-12-2007, 07:57 PM
Oops, sorry Stomp; I must've posted concurrently - didn't mean to cut you off.
Lol, that's cool....i kept my edit window open while I was away...so I didn't even notice your post ;-)
@originaliron - both of our codes should work for you.
Sekhar
10-12-2007, 07:59 PM
You have a function timer(event:TimerEvent):void - I guess you meant moveObject().
stompwampa
10-12-2007, 07:59 PM
This is what I have. I am very new to ActionScript. Thanks for the posts. What am I missing in my code?
Whatch your case...it is case sensitive.
timer.addEventListener(Event.Timer, moveObject);
//SHOULD BE THIS:
timer.addEventListener(TimerEvent.TIMER, moveObject);
//as Sekhar pointed out, your timer function should look like this:
function moveObject(event:TimerEvent):void
{
balloon_mc.y -= 2;
}
originaliron
10-12-2007, 08:04 PM
Well that fixed it. I cannot thank you two enough. thanks for the quick fix.
Do you have any good online resources for learning ActionScript 3.0? I am kind of learning as I go, thanks to people like yourselves.
Thanks
stompwampa
10-12-2007, 08:06 PM
lynda.com
$25.00 per month. You can't beat it. The tutorials are the best of seen, and they're meant for beginners. As I've said in several other threads, it's well worth the money.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.