Copy link to clipboard
Copied
I have an app where the user has to click and drag an item to the correct spot. I want the user to be able to use the keyboard to do this. I want them to click A, b, c, d, to activate a particular spot then click 1 2 3 or 4 to tell them where to drop it. If not this kind of functionality any other suggestions are welcome. (i.e. move the across the stage using the arrows., etc.)
It's basically a drag and drop application.
Copy link to clipboard
Copied
you can use keyboard listeners
window.addEventListener("keydown", downF.bind(this));
window.addEventListener("keyup", upF.bind(this));
Copy link to clipboard
Copied
Hi.
We need more details to suggest a better code, for now I would just like to suggest an approach for you to get started:
// this sample assumes that you have pieces in the main timline named like pieceA, pieceB, pieceC, pieceD...
// it also assumes that you have slots on stage named like slot0, slot1, slot2, slot3
var piecesKeys = [ "a", "b", "c", "d" ];
var slotKeys = [ "1", "2", "3", "4" ];
var piecePrefix = "piece";
var slotPrefix = "slot";
var selectedPiece, targetSlot;
function main()
{
window.addEventListener("keydown", onKeyDown);
}
function onKeyDown(e)
{
if (piecesKeys.indexOf(e.key) > -1)
{
selectPiece(0);
selectedPiece = root[piecePrefix + e.key.toUpperCase()];
selectPiece(1);
}
else if (slotKeys.indexOf(e.key) > -1)
{
if (selectedPiece && !selectedPiece.inPlace)
{
targetSlot = root[slotPrefix + e.key];
if (!targetSlot.hasPiece)
{
selectedPiece.x = targetSlot.x;
selectedPiece.y = targetSlot.y;
selectedPiece.inPlace = true;
targetSlot.hasPiece = true;
}
}
}
}
function selectPiece(frame)
{
if (selectedPiece && !selectedPiece.inPlace)
selectedPiece.gotoAndStop(frame);
}
window.root = this;
main();
I hope it helps.
Regards,
JC
Copy link to clipboard
Copied
This is good code JoãoCésar I may use it myself.
Copy link to clipboard
Copied
Sorry for the delay. Here is better explanation. My code is not beside me at the moment but here is some of it.
The overall application that I am trying to make is an application that dynamically draws a line from one point to another. I have the line but i need to add an alternative method for accessibility. Similar to a drag and drop but instead of dragging the entire shape a line is created and dropped on the square.
When the user hits "A" it should represent circle0 then if they hit 2 that would create and drop the line on the 2 square.
self.circle0.on("pressmove", lineBegin); //User drags the line
self.circle0.on("pressup", lineEnd); //User releases the line over a target
self.square0.on("pressmove", moveLine); //User moves the line from one target to another
self.square0.on("pressup", lineEnd); //User releases the line over the target
self.square0.on("mouseover", readyHit); //User hovers over a target
self.square0.on("mouseout", unreadyHit); //User not hovering over a target
self.circle0.on("click", lineBegin); //User selects the target that will evetually be the drag point
self.square0.on("click", moveLine); //User removes the line from a target to place it on another