PDA

View Full Version : Assignment of sprite variable name at runtime


Excel
11-25-2008, 09:21 AM
I have a requirement wherein I have to draw some of the elctronic components like resistors, capacitors and inductors. I have tried using the image and converted it to the Movieclip, but it has some limitations with respect to our requirement and also doesnt look natural. Is it possible to draw the components using basic shapes and then try converting it to movie clip. I want to assign some events to the ends of the components(small end wires for connecting the components) say like highlighting the wire end by drawing a circle on mouse focus on these points. This requirement is not possible with the image being converted to movieclip. Please Advise.

Regards,
Excel

Sekhar
11-26-2008, 04:04 AM
I'm not entirely sure what you want, but my guess is button will serve you best. With a button you can create different images for the "up" state (normal), "over" state (when mouse goes over it), etc. So, when you move the mouse over the wire, the wire will change to wire with a circle. And the images can be whatever you want for each of these states - raster images or stuff you create using primitives.

Excel
11-26-2008, 05:10 AM
Thanks for the advice. I wish to elaborate more on the requirement. The resistor which looks like an oval with a little bit if wires extending from it on both the sides od the oval. The compoent has to be picked from the tray and dragged on the breadboard(A matrix os small squared slots), while on dragging on the breadboard the left end wire senses the squard slot on the bread board and try to get fixed to that slot if that is not the desired slot for fixing the component the user drags it further towars the required slot, the movement of the left end wire is not smooth and just moves over the breadboard fixing to the nearby squared slot all along in somewhat an intermittent way. We have tried convrting an bmp image to movieclip which doesnt suit my requirement.

Regards,
Excel

Excel
11-26-2008, 12:41 PM
Is this requirement not achievable in AS 3.0??

Regards,
Excel

Sekhar
11-26-2008, 04:22 PM
OK, I guess what you want is snapping, where the resistor end will snap to the nearest slot on the breadboard. And, if the slot accepts a resistor, it'll show that (with a circle or whatever).

You can certainly do this in Flash. On mouse down, set the resistor clip as draggable. Then listen for mouse move event and snap the resistor to the nearest slot based on the mouse position (you know exactly where the slots are since it's a grid). And since you know what the current slot is, you can figure out if that slot accepts a resistor so you can show the circle. Remove draggable on mouse up, and you're set.

Excel
11-27-2008, 08:54 AM
But the real problem is that the mouse pick point on the component is different from the left wire end. and mouse pick point can vary on different pick of the same component. We have already done the resistor clip as draggable and still we are not able to achieve this.

Is there a way wherein I can listen to the movements of the left end of the wire which is part of the movie clip.

Regards,
Excel

Sekhar
11-27-2008, 07:14 PM
Hmm, still can't quite make out what you want... Anyway, I've created an example for you (my undergrad was in Electonics, so I guess I have a soft corner for foks working with breadboards), see below. It should answer all your questions.

Excel
11-28-2008, 12:43 PM
Thanks a lot for the response. We have already built the breadboard the same way you have built, the problem is instead of a rectangle with 2 straight lines representing the wire ends we are using the bitmap of the component converted to movieclip whose ends doesnt move the way the end wire in your example moves snapping to the breadboard slots.Let me go by the way you have designed the component. Lets assume that the component width is more than the distance between any 2 slots. In this scenario lets take that the left end of the component is the reference point and is the first to be fixed on the breadboard slot, so for this we need to keep track of the postion of the left wire end of the component. When once the left end is fixed the right ended wire should now be extended to the slot where the user desires it to be connected.
To summerise
1. The main issue here is "How to track the left wire end of the component
on the breadboard?".
2. How to extend the right end now to connect it to the desired slot?

Prior to all these the component is placed in a component tray, from where the user has to pick. I have seen in your example that you have drawn in prior the picture of the component(A rectangle with 2 straight lines on either sides) and added it to library. When we run the example we see the component already on the breadboard from where we pick and move it around, but in our case we have to pick from the component tray and every picked component is a new instance(Any no of resistors should be created by just dragging the component in the tray and the tray always looks like there is still resistor which can be picked) and all having the above behaviour. The question here is

"How to achieve multiple instances"?. What way is best to achieve this(Either bitmap way OR the way you have shown in your example)

The thank you(Sekhar) once again for your patience hearing to me on my requirement. Thanks a lot.
Regards,
Excel

Sekhar
11-28-2008, 08:34 PM
If you make the left end the pivot of the component (like I did in the example), you don't need to separately track the left end - its x = component's x.

But if you want to extend the right end, one way is to make the resistor itself an image (raster or vector) and dynamically draw the ends as two separate line elements using the Graphics interface. That way, when you move the right end, you can dynamically draw the right lead.

WRT the number of instances, it doesn't matter how many you add. When the user selects the component from the library on the page, you instantiate and assign the code like what I showed in my example. Whether you have one already on stage or you add multiple instances from a library makes no difference - they all behave the same way.

And since with breadboards you can have overlaps (multiple components sharing the same end), you don't even need to check for overlaps (unless this is a separate requirement for you).

Excel
11-29-2008, 06:21 AM
Thanks for the reply. The code is very simple and compact but yet very powerful in action. The basics are well understood now. As what I understand from your code whichever may be the place on the component the mouse click is the left end is moved to the rounded value of the x and y co-ordinate of click. In my case as told you the right end can be extended to any slot on the breadboard, so when the component is in horizontal position on the breadboard and fixed on its left end, when the user tries to connect the right end of the component to the breadboard slot which is vertically above the left end, the resistor image has to turn vertically. If the user wishes to fix the right end at say around 60 degree(to the left end on the breadboard) the component has to turn by 60 degree, so for this requirement we cant rely on statically drawn components, we have to dynamically draw the components as and when the user requires. How to achieve this requirement. Thanks once again for your help.

Regards,
Excel

Excel
12-03-2008, 04:40 AM
We have a requirement wherein, we are drawing circles using drawCircle graphic function and their centre points assigned to sprite variables. We call the centre of this circle as "CircleCentr". The compulsion here is that we need to create so many circles and we need to name the centre of these circles as "CircleCentr_1", "CircleCentr_2" ...and so on. Please advise me how to go about.

Regards,
Excel

LeenieGames
12-03-2008, 05:27 AM
Assigning variable names at runtime is not the answer. Sprites (and all other DisplayObjects) have names. You can give your Sprite a name that can be used to reference it later on.

var currentIteration:int = 1;
var newSprite:Sprite = new Sprite();
newSprite.name = "CircleCentr_"+currentIteration;
currentIteration++;

Excel
12-03-2008, 05:57 AM
Thanks for the reply, The problem is that we are adding the circle (sprite object) as child to another object(say line) of which we can have multiple instances. The thing is that on the stage when I pick the line say from a basic shapes tray the child of the previously dragged line object(Circle attached at the end of line) will come and attach to the new one and the same thing happens for the next new object dragged. This is the problem we are facing with the same logic as given below. Is there a way to add specific instance of the sprite object to be added to a specific instance of another.

CyanBlue
12-04-2008, 03:28 AM
Please do not create multiple threads with the same question... We don't allow bumping in this forum...