View Full Version : if selected, then do this (function)
brewthom
01-09-2008, 05:16 PM
Hello everyone. I'm trying to write a function (or class?) that will say "do this to the last movie clip clicked on". I posted this earlier as "basic gui problem". Ex: if I have 20 clips on the stage, I want the function to run on whatever clip was last selected. I also have a drag class attached to each clip. My brain is stuck here. (designer -- not a programmer). Ideas? I'm assuming I need something like "if this clip is at the highest depth, then do this". (clips selected are at highest depth as they are draggable????)
xwielder
01-09-2008, 06:24 PM
There are a number of ways you can do this. I personally prefer the string holder method. Create a string holder: var myHolder:String = "";. Then just update that string everytime an object is selected: myHolder = event.target.name;
This way, myHolder will always hold the instance name of the last object clicked. Follow?
brewthom
01-09-2008, 07:08 PM
Hmm. I'm not sure. What I want is to have different buttons change the selected movie clip. In that situation-- what IS the event target. Here is a snippet of my code. Perhaps that will clarify.
function alphaDown(evt:MouseEvent):void{
line_mc.alpha-=.02
}
//alpha
alphaUp_btn.buttonMode = true;
alphaUp_btn.addEventListener(MouseEvent.MOUSE_UP,a lphaUp);
alphaDown_btn.buttonMode = true;
alphaDown_btn.addEventListener(MouseEvent.MOUSE_UP ,alphaDown);
stompwampa
01-09-2008, 07:49 PM
can you explain a little more what you're trying to do? If each button is perorming a different funtion, than you may be better off just writing the code for each function....I'm a little confused as to what you're trying to do...
brewthom
01-09-2008, 08:02 PM
Here's what I'm trying to do: add mark adds another clip. THEN , I want the buttons to control THAT mark. They now just control the first one. You can see this in action (more or less) at http://www.brewerthompson.com/testing/ (http://www.brewerthompson.com/testing)
Thanks!
DocWatson
01-09-2008, 08:30 PM
I had a similar issue recently regarding a colorpicking class I created. This class (ColorGrid) created no less than 216 instances of ColorBox. The issue with mine was that as they were created, only the last one made would return anything, as it was considered "this" colorBox. The solution was thus:
in the class definition, before public function ColorGrid(...), I placed this:
private static var box:ColorBox;
public static function setBox(sb:ColorBox):void {
box = sb;
}
box was the instance I used to create more ColorBoxes (I used a for loop to say box = new ColorBox(...); for each object). Since it's static, only one will belong to the ColorGrid class at any time. The setBox function takes in a colorBox and sets that box to the one selected.
So, inside ColorBox I placed this:
package SuperDraw {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class ColorBox extends Sprite {
...
private var square:Sprite;
public function ColorBox(blah...) {...
square = new Sprite();
square.graphics.lineStyle(this.borderThickness, this.rollOutBorder);
square.graphics.beginFill(this.boxColor);
square.graphics.drawRect(0, 0, this.boxDimension, this.boxDimension);
square.graphics.endFill();
addChild(square);
...
square.addEventListener(MouseEvent.CLICK, mouseClick);
...
}
}
public function mouseClick(event:MouseEvent):void {
ColorGrid.setBox(this);
}
What that CLICK listener does is set whatever box was clicked on to be "this" box of the ColorGrid. Clear as mud?
brewthom
01-09-2008, 09:02 PM
Thanks Doc. I'll give that a whirl. Pretty technically savy for a flatpicker.
DocWatson
01-09-2008, 09:19 PM
Thanks Doc. I'll give that a whirl. Pretty technically savy for a flatpicker.
haha :P
I do, by coincidence, also play guitar. I'm no Doc Watson though.
Er...
brewthom
01-09-2008, 11:28 PM
Still not clear on exactly how to manage this. I'm not writing separate classes (except my clipdragger). Working up to that. I'm just not sure how (within my functions) how to indicate that the rest of the functions should apply to the movieclip last selected. (clear as mud, right? see web example at http://www.brewerthompson.com/testing/. Any other ideas or clarification? I've written a selectThis function but can't get my head around how to indicate this in the code. Help or pointers to a good tutorial appreciated. Just learning AS3. Patience appreciated.
DocWatson
01-09-2008, 11:36 PM
how do you make the new marks?
What I mean by that is do you have some Sprite variable or anything similar that creates them?
brewthom
01-09-2008, 11:42 PM
Here's what I'm using. There is probably a better way to do this?
var newLine:MovieClip = new Line();
function addClip(evt:MouseEvent):void{
newLine.x = newLine.y = 100 + inc;
addChildAt(newLine,0);
inc++;
}
//add button
add_btn.buttonMode = true;
add_btn.addEventListener(MouseEvent.MOUSE_DOWN, addClip);
DocWatson
01-10-2008, 01:27 AM
looks like you aren't actually creating new instances of newLine, just moving it and re-displaying it.
Try this instead:
var newLine:MovieClip;//declare newLine as a movieClip
function addClip(evt:MouseEvent):void{
newLine = new MovieClip();//Create a new instance of newLine
newLine.x = newLine.y = 100 + inc;
addChildAt(newLine,0);
inc++;
}
//add button
add_btn.buttonMode = true;
add_btn.addEventListener(MouseEvent.MOUSE_DOWN, addClip);
you might need to take out var newLine:MovieClip() from the main function, and instead place it in the class definition as private/public static newLine:MovieClip();
Then you can use the method I described. It might be a lot easier though, to make a new class that creates/handles your newLine object. Like this:
//File: newLine.as
package WhatYouNamedYourFirstFile {
...//Whatever imports you need
public class newLine extends MovieClip() {
private var anObject:MovieClip;
public function newLine(x:Number, y:Number .../*any additional parameters here*/...) {
anObject = new MovieClip();
this.anObject.x = x;
this.anObject.y = y;
}
}
}
You would also need to put in code that used whatever image to make the mark is in the class. I didn't actually test this code or anything, so it's pretty barebones and probably not very robust
brewthom
01-10-2008, 01:29 AM
You rock DocWatson. I will try that and let you know.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.