AS3 dress up game drag & drop not working - actionscript-3

I'm making a dress up game and ran into the following issue.
I know how to make things drag and drop, however whenever the movieclip is situated in a layer below another, it will not let me drop it / does not register the mouse release.
In this case for example - The very top layer is the doll's hair. Under this layer are articles of clothing, and under that is the doll base. Like so : https://gyazo.com/a0ea8eae2ee5505874f4207a888249bf
I can pick up a top from the inventory but cannot place it on the doll because my cursor is over the hair from the very top layer. In a way, it seems like the hair layer is acting as a barrier between the cursor click and the article of clothing. Is there any way around this?
Here is a link to the game as it is now - i'm sure you will see the issue quickly if you attempt to move things around.GAME LINK
Here is the code for one of the clothing layers, if needed.
tshirt.addEventListener(MouseEvent.MOUSE_DOWN, drag17);
tshirt.addEventListener(MouseEvent.MOUSE_UP, drop17);
function drag17(event:MouseEvent):void
{
event.target.startDrag();
}
function drop17(event:MouseEvent):void
{
event.target.stopDrag();
}
Sorry for the rather crude explanation, and thank you in advance!

the problem
You are exactly right about the hair getting in the way.
method 1
One way to fix that would be (assuming you dropped the hair there):
function drop17(event:MouseEvent):void
{
event.target.stopDrag();
event.target.mouseEnabled = false;
}
In your hair example, the hair needs to have mouseEnabled set to false so that it won't be able to be your target by mistake on a mouse event. If the hair wasn't "dropped" there, set mouseEnabled to false for the hair wherever it makes sense to do so... maybe when it is first constructed or instantiated.
method 2
An alternative method is to force the item that you picked up to come to the front of the z order. Like this:
function drag17(event:MouseEvent):void
{
addChild(event.target); // this doesn't add another Sprite or movie clip, it just moves it to the top of the z order. Trust me.
event.target.startDrag();
}
With your dragging element now at the top, nothing will be "between" it and the mouse. This method probably looks cleaner since the thing you are dragging isn't getting lost behind other things. This will force you to deal with the eventual need to place the item in the correct z-order when you drop it, but that could easily be done by doing addChild to a clothesContainer or something like that. That would be another question though.

Related

AS3 Swap depths in Flash IDE without destroying movieclips

I have two movieclips, one is on top, one is on bottom. The top one is set to the second frame. I move it to the z axis bottom on the next frame, and for the remaining frames it plays all frames like it has been reset without any code (like I didn't tell it gotoAndStop(2)).
Image example: http://i.imgur.com/mQ8f5uT.gif (hard to tell by the frame rate, but the dark green box starts playing as if I didn't set it's frame once I move it below)
I know it doesn't have that issue when I code the depth swapping with setChildIndex, but the animations are lost.
If you're wondering, it's a simplified issue of a larger moving character sprite animation that I would like to swap depths of movie clips in the animation without resetting the movie clips underneath the depth change. So I'm not really looking for a work around per se, unless there's a well design solution and not a band aid "hack."
I think I didn't understand your question fully. A MovieClip will keep playing or keep being stopped no matter what you do with it, even if it was removed from the scene. I made a simple example which shows that MovieClip state is not affected by depth change.
The working SWF is here to test: http://zdg.ru/tmp/updown.swf
And the code is:
import flash.events.MouseEvent;
var is_playing:Boolean = true;
btn_toggle.addEventListener(MouseEvent.CLICK, doToggle);
btn_updown.addEventListener(MouseEvent.CLICK, doUpdown);
function doToggle(evt:MouseEvent) {
if (is_playing) mv_test.stop(); else mv_test.play();
is_playing = !is_playing;
}
function doUpdown(evt:MouseEvent) {
var tmp:int = getChildIndex(mv_block);
setChildIndex(mv_block, getChildIndex(mv_test));
setChildIndex(mv_test, tmp);
}

Overriding the x getter in Actionscript 3.0

I have a class called Element. It is basically my ultra-movieclip, and nearly all classes in the game subclass it. Element extends Movieclip
The code in the game is built around the idea that all Movieclips are registered at the top-left corner. Register it somewhere else, and everything starts to fall apart.
The problem is, sometimes the in the animation and such, the top-left corner moves, but the registration point doesn't, and you end up with a registration point somewhere in the middle of the Movieclip. This causes problems.
So, how do I fix this problem? We can find the true top-left corner of a Movieclip using getBounds (this is the documentation for it), but using getBounds over and over in code would get kinda tedious.
My idea is to override the x getter in the Element class. That way, we can always know where the Element's true top-left corner is, without having to write a bunch of complicated code every time!
And for the sake of argument, let's assume that just making a new getter like "trueX" will open a trapdoor beneath my feet and plunge me into a cage of starving gophers.
I cannot figure out how to override the x getter! How can we do this? Have I missed an incredibly simple solution to the problem that has nothing to do with overriding?
override public function get x():Number {
//I have no idea what I'm doing!
}
This should be all you need to do: (if I understand your question right)
override public function get x():Number {
return super.x + getBounds(this).x;
}

Flash: AS3 – Bring symbol to front on rollover within movie

I have created a map of Europe in Flash using AS3. I am an absolute beginner. Each country has been converted into a movie (using lasso tool). These movies are on one layer (with slight outline visible) on main timeline. On the other main timeline layer is the original outline map of Europe.
I have managed to make it so when a country is rolled over, a box tweens in scale to full size (to later insert text in).
I need to make it so that when each country is rolled over, that country's text box (which appears on Roll_Over) comes 'to the front'.
At the moment a couple of countries' text boxes work perfectly (ie: filled with white and completely visible), but most are either buried beneath the main map, or threading randomly through the faint movie clip outlines (which are at the front).
I'm guessing that this has to do with the display list. The countries that work perfectly are probably at the top. But how do I make it so that the country rolled over immediately goes to the top and returns on Roll_Out?
This would complete my map so any help would be VERY MUCH appreciated.
I have used following code:
//FRAME ONE
this.stop();
movieClip_6.addEventListener(MouseEvent.ROLL_OVER, fl_MouseOverHandler_15);
function fl_MouseOverHandler_15(event:MouseEvent):void
{
gotoAndPlay(2);
}
movieClip_6.addEventListener(MouseEvent.ROLL_OUT, fl_MouseOutHandler_67);
function fl_MouseOutHandler_67(event:MouseEvent):void
{
gotoAndStop(1);
}
//SECTION BELOW IS MY PROBLEM
movieclip_6.addEventListener(MouseEvent.ROLL_OVER,Rollover,false,0,true);
function Rollover(event:MouseEvent): void
{
setChildIndex(MovieClip(e.target),this.numChildren-1);
}
Many thanks in advance.
You can try calling addChild() again to the DisplayObject/Movieclip to bring it to the front. Instead of:
setChildIndex(MovieClip(e.target),this.numChildren-1);
Try going:
addChild(MovieClip(e.target));
You can use "layers" to bring objects in front of another.
constructor:
//Create the front layer
var frontLayer:Sprite = new Sprite();
//put all text boxes into front layer
frontLayer.addChild(textbox1); //etc...
//Add layer to mc
addChild(frontLayer);
rollover pops up:
textbox1.visible = true;
pop down:
textbox1.visible = false;

XY restrictions for custom mouse cursor in Actionscript3

I have this interactive 5 seconds animated intro for a website. the preloader and one item are animating and i made the second animation follow the mouse cursor but it has to stay within a certain part of the stage to work with the other animation happening on screen.
I have this code on the movie clip
Mouse.hide();
potistiri.addEventListener(Event.ENTER_FRAME, newCursor);
function newCursor(event:Event): void { potistiri.x = mouseX;
potistiri.y = mouseY; }
and i like i said i just want it to stay in the area i want...
i found this code which gives me errors for not putting the staments if and else if correctly or that it needs a rightparen when i input my numbers in...
if(this._x>Stage.width){
this._x=Stage.width;
}else if(this._x<0){
this._x=0; }
but i cant get it to work...
i need it to move between x 208-656 and y 140-336 and when it gets out of that area the object stay there doing its loop and you see the normal mouse cursor moving in the rest of the screen.
thanks a lot in advance...im leaving my it to the experts in here to pls help me ouy!
The logic you're using in your if/else is fine for clamping the movie clip to a specific area, what exactly do your errors say?
In regards to seeing the normal mouse cursor again you could try using the same if/else checks to determine whether the mouse should or should not be hidden ie if the mouse is outside the area and is hidden, call Mouse.show(), else if it is inside the area and shown, call Mouse.hide().

Drawing a series of isometric enemies in the correct order or dealing with the dirty rectangle?

I'm wonderring if anyone can help me with making sure my... uhhh ... Z-index (bad pun, you'll see why in a moment) is in the wrong order. I've been doing this for a few hours straight now and my eyes are going buggy - but - maybe leaving a question on Stack overnight will help push this in the right direction.
I've been working on the code for https://github.com/AlexChesser/jsSprite and I'm as far as the 6th test. Use the W key to run, A and D to turn left and right: http://chesser.ca/jsSprite/06-brainnsss....php (Gettit? Z-Index?! Hilarious).
Anyways, You'll notice that if you run around the screen for a bit. the individual Zombies' white squares / dirty rectangles overlap the other zombies' squares. When working with multiple overlapping sprites, how does one go about making sure they all get drawn without upsetting any of the other sprites?
(You see z is for zombies, but z index like when you're dealing with overlapping in CSS - probably way funnier when you've been coding for a number of hours straight).
Thanks for your
Brainsss......
It's not a z-index issue, your zombies themselves are okay.
Your problem is really with the second line of drawFrame
drawFrame: function(){
Sprite.ctx.clearRect(0,0,Sprite.width,Sprite.height); //clear previous frame
// I am trouble:
MainContext.clearRect(Sprite.Xpos, Sprite.Ypos, Sprite.width, Sprite.height);
It is clearing a rectangle of the main canvas where the zombie once was every time you draw a zombie, which can affect nearby objects!
So instead you should be clearing the entire canvas each time.
Try commenting out the MainContext.clearRect in drawFrame, and instead add one in runloop like below. It should fix your problems.
runloop = function(m) {
// New clear put here!
MainContext.clearRect(0,0,canvas.width,canvas.height);
m.drawFrame();
for (Z in Zarr) { // For ZOMBIE in "Zombie Array" Aaaaarrrgghhh...
Zarr[Z].pointTo(m);
Zarr[Z].drawFrame();
MainContext.drawImage(Zarr[Z].canvas, Zarr[Z].Xpos, Zarr[Z].Ypos);
};
MainContext.drawImage(m.canvas, m.Xpos, m.Ypos);
};
How about sorting your array (Zarr) by the y coordinate Ypos of each zombie before rendering? Or are you getting at the problem with (a lack of) transparency?