It sounds like a reasonable straightforward code of startDrag and stopDrag with a hitTest, however this is not the case.
Since my inventory items are inside movieclips, this has made the process a lot harder.
Here is the code I have created:
| //moveLightbulb var lightbulbMove:lightbulbClass = new lightbulbClass(); stage.addEventListener(Event.ENTER_FRAME, lightbulbMoveListener); function lightbulbMoveListener(event:Event):void { if(lightbulbGrab==1){ trace("Lightbulb grab is grabbed (stage check) | " +lightbulbGrab); addChild(lightbulbMove); lightbulbMove.startDrag(); lightbulbMove.y=mouseY; lightbulbMove.x=mouseX; lightbulbMove.z=1; lightbulbMove.visible=true; cursor_mc.visible=false; stage.removeEventListener(Event.ENTER_FRAME, lightbulbMoveListener); stage.addEventListener(Event.ENTER_FRAME, lightbulbReleasedListener); lightbulbGrab=2; } } function lightbulbReleasedListener(event:Event):void { if(lightbulbGrab==3){ releaseLightbulb(); } } function releaseLightbulb() { lightbulbMove.stopDrag(); lightbulbMove.visible=false; cursor_mc.visible=true; cursor_mc.startDrag(true); lightbulbGrab=0; trace("Lightbulb grab is reset (stage check) | "+lightbulbGrab); stage.removeEventListener(Event.ENTER_FRAME, lightbulbReleasedListener); stage.addEventListener(Event.ENTER_FRAME, lightbulbMoveListener); } // hittest lightbulb and light; stage.addEventListener(Event.ENTER_FRAME, lightbulbHitLampListener); function lightbulbHitLampListener(event:Event):void { if (lightbulbMove.hitTestObject(tableView.tableViewLamp)) { lightbulbInserted=true; if (lightbulbInserted) { tableView.tableViewLamp.gotoAndStop(2); trace("Lightbulb grab is released and inserted (stage check) | "+lightbulbGrab); lightbulbGrab=3; stage.removeEventListener(Event.ENTER_FRAME, lightbulbHitLampListener); } } } |
The code above would use the EventListeners to look out for when specific user tasks have been completed. To do this I used if statements on integers
The integers numbers represented the following:
0 - default
1 - grabbed
2 - released
3 - dropped
The reason why default is so important is because it has not yet been grabbed, and it means that if the lightbulb was released onto the stage but not directly on the lamp, it would appear back in the default position - in the inventory.
The user event tasks were scripted in the inventory.
The reason for doing this is because I would not know which of the inventory slots the lightbulb was in. So I would therefore do it on the frame of the inventory movieclip that the item is in.
I used the following code inside in the inventory:
| lightbulbInv.addEventListener(MouseEvent.MOUSE_DOWN, grabbedLightbulbListener); function grabbedLightbulbListener(evt:MouseEvent):void { if (MovieClip(root).lightbulbGrab==0) { trace("this should work at 0 only"); MovieClip(root).lightbulbGrab=1; trace("lightbulb grab is grabbed (inventory check) | "+MovieClip(root).lightbulbGrab); lightbulbInv.visible=false; } } stage.addEventListener(MouseEvent.MOUSE_UP, notgrabbedLightbulbListener); function notgrabbedLightbulbListener(evt:MouseEvent):void { lightbulbInv.visible=true; if(MovieClip(root).lightbulbGrab==2){ trace("Lightbulb grab is released (inventory check) | "+MovieClip(root).lightbulbGrab); MovieClip(root).lightbulbGrab=3; trace("Lightbulb grab is dropped (inventory check) | "+MovieClip(root).lightbulbGrab); //lightbulbInv.visible=false; lightbulbInv.addEventListener(MouseEvent.MOUSE_DOWN, grabbedLightbulbListener); } } stage.addEventListener(Event.ENTER_FRAME, lightbulbUsedListener); function lightbulbUsedListener (event:Event):void{ if(MovieClip(root).lightbulbInserted){ stage.removeEventListener(MouseEvent.MOUSE_UP, notgrabbedLightbulbListener); gotoAndStop("itemNothing"); } } |
Both of these codes have so far been the hardest to write in this project.
I found this on drag and drop of items
http://www.actionscript.org/forums/showthread.php3?t=151862
No comments:
Post a Comment
Note: only a member of this blog may post a comment.