Draw letters based on points on mouse click in action script 3 - actionscript-3

I asked to draw line between objects in stage (e.g points) on mouse click over 2 of these objects
example : if I want to write 'A' i need to set 5 point and connect them by clicking all these points like:
*
* *
* *

This question isn't very specific. Which part are you having problems with? The Graphics object provides methods for drawing lines. It's very easy to draw between points with drawTo(...)
For a quick intro see: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7dd9.html

First, you have to detect the user's mouse click and convert that into some sort of form of data that you can use to draw a line with.
Use the MouseEvent class and EventListeners to detect a user's mouse click. If you don't know how to use event listeners, refer to here.
The dispatched MouseEvent object has a stageX and stageY property which refers to the X and Y coordinates of where the mouse click occurred. These two pieces of data can be used to create a Point object. Instantiate and store this point object somewhere convenient - so that when you have more than 2 of them, you can then refer to AndySavage's answer on how to draw a line with those two Point objects.
Basically, you want to create a Point object every time a mouse click occurs, and then check internally the amount of Point objects you have. If you have one, don't do anything, because you need two points to draw a line. If you have more than one, then use the two latest Point objects you obtained to draw a line.
Tip:
If you store the points into a vector, then every time a mouse click occurs, you can push a Point object inside. Then, check the length of that vector, and if it is greater than 1, you can access the last two elements by using the it's length - 1 and length - 2 as indices.
You can read the documentation on the Vector and Point classes by searching on google. (Type something like "as3 api Vector") I'd add links but I can only add less than two per post right now.

Related

Can I use **hitTestObject** with only the inside part of an object?

I want to use hitTestObject with only the inside (visual) part of the object, not the outside part or the border.
How can I use that?
By default, hitTestObject checks only the bounding box of the specified object to check for collision, which is the "outside" part you're referring to. There's another function, hitTestPoint() which checks whether a given point collides with another object, and hitTestPoint allows you to check for rough collisions (only the bounding box) or for precise collisions (the "visual" part only); however, it only works for a given point.
So, if you have two objects and you want to check whether their visual parts overlap, and not their bounding boxes, then you have two options:
1) Check only one point, e.g. the x and y coordinate of one displayObject, with the entire shape of the other displayObject, or
2) Create an invisible "border" of separate displayObjects which reside in the first displayObject, and check whether any of their coordinates collide with the "visual" part of the second displayObject.
In either case, you will have to use hitTestPoint(); this takes Stage coordinates, so you'll have to use localToGlobal() to convert it.
//where shapeAa is a shape nested within a displayObject 'shapeA', and you want to check whether shapeA collides with shapeB
var globalPt:Point = shapeAa.localToGlobal(new Point());
shapeB.hitTestPoint(globalPt.x, globalPt.y, true);
//...repeat for other shapes nested within shapeA, along its border, i.e. shapeAa...shapeAz
There's various libraries that allow you to test precisely for collision (and if you're using BitmapData instead of Sprite/MovieClip/etc. then you can check whether the objects accurately collide without the need of such libraries).

Corona use object properties from Tiled layers

I am new to Lua scripting, and game development. So please I am just a noob in Lua.
I have searched the net for solutions to my problems, without any luck.
I use Photoshop, Corona, Dusk, json and Tiled on windows7.
I am creating a "board" like game, i.e. Setlers. I am using a world map, as the background. The background image of the game area is a world map (world.png file). I have no problem here.
I would like to create transparrent clickable objects matching the countrys borders on my gamemap with all parameters and values (I have added in Tiled) stored in the object. So When the player clicks on the country the transparrent object (on top of the map) is the one clicked and an eventlistener acts on the click.
In Tiled I can create all the objects I need, naming them + assigning parameters and other values.
If I add object.alpha value in Tiled, the alpha value is passed on to corona and working there.
How can I read these data from the json/tmx file in Corona and adding them to a lua table?
The way I am thinking to use the Tiled map and its objects, is to create one polyline trace of each country’s border (creating one object per country). Then place each “country traced object” on top of the world.png map, also naming the object with the countrys name like “object.name = TileBritannia” and also the other properties for use in game.
My problem is getting the objects info, like object.name, and an eventlistener reacting to a click on the object.
Is a polyline the right way to create a clickable area on a map, when I use a png file as a background image?
What is the best way to create a country border objects, in one layer or with all countries as individual object layers in Tiled.
Can I create one layer with sub objects and still access them in my code?
How do I get the object name and other properties, set in Tiled.
When I try to use the (local britannia = tiledMap:load("britannia.json")) the "load" is not working, getting a nil value.
I am looking for a code that will extract/get/read the object.name i.e. “objBritannia” or "TileBritannia". from the json/tmx file.
When I try to read the different parameters from the json file, I don't get the result I expect. I get the result = function: 046A73B0, was hoping for an object name of some sort.
Please provide links to or code example.
I have edited the question.
Thanks
For questions 1 and 2: I have not used Tiled, but based on Corona Tiled, you have the right strategy in mind. That page makes me think that you can just use tap event listener to detect tap. If you are having issues with the example on that web page, please update your question to be more specific. If tap event handling doesn't work (maybe you're talking about a different Tiled lib), look a Polygon fill and Point in Polygon detection, because that's basically what you need to do. Try some stuff from there. If it still doesn't work for you, then update your question with specifics otherwise it will be likely get closed (it is a little too broad as it is).
For #3, Lua is a dynamic language that supports adding properties to objects in one line. So upon the example on the Corona Tiled page, all you would have to do is
tiledMap = require("tiled")
local britannia = tiledMap:load("britannia.json")
britannia.name = "Britannnia"
local Zulu = tiledMap:load("zulu.json")
zulu.name = "zulu"
Naturally you will probably have a whole bunch so you will create a function that you call for each tile. It's not clear what map.layer["objBritannia "].nameIs("TileBritannia") is supposed to do so I can't comment.

As3, OOP strategy for custom class DrawVectorLineArt

I am doing some math projects that require a lot of vector line art--that is a line drawn between to points with a circle at the start point and an arrow at the end point. A call to Math.atan2() keeps the arrow aligned. I call this class DrawVectorLineArt() and it creates instances of 2 other custom classes DrawArrow() and DrawCircle().
So far so good--DrawVectorLineArt() draws just what I need. Now I need to animate the vector art.
So in a function onEnterFrame I want to update the postion of arrow and circle, the objects created by DrawArrow() and DrawCircle(), respectively. I also need to clear and redraw the line drawn between them. At this point I am not sure how to proceed in an OOP framework. Do I need to create methods of my custorm class DrawVectorLineArt() to update the position of arrow and circle and subsequently clear and redraw the connecting line?
Any advice or links appreciated. Thanks!
"Do I need to create methods of my custorm class DrawVectorLineArt() to update the position of arrow and circle and subsequently clear and redraw the connecting line?"
Yes.
The arrow and the circle are very members of DrawVectorLineArt, and going by its name and choice of members, so should the line (if it's implemented through actual data). DrawVectorLineArt should contain and implement the whole animation between the circle, arrow, and line. As such, if the animation's supposed to be able to change after creation, the same instance of DrawVectorLineArt should be able to take any two legitimate points supplied to it (or that it becomes aware of internally, depending on what you're doing), reposition the three components, and turn the arrow and line appropriately, within its own code.

AS3 - Hit Test Object between two Movieclips in the same class file

I'm wondering if there is a method to do a hit test object between two movieclips from the same class. The movieclips are being spawned into the stage using a for loop.
Is it possible to if(item(event.currentTarget).hitTestObeject(item(thats not being selected at the moment?))) I'm doing a drag and drop game and am trying to not let the items stack above each other when dragged to the snapping slots on the stage. If not is there other methods avaliable? Thanks in advance (:
first of all, detecting, whether two display objects (sprites, movieclips, etc.) hit each other, is independent of the fact, that the objects are instances of the same class. The important thing is, you have two instances and they are display objects, so you're good to go.
If you want to do hit tests, then i would do the following:
Create a helper class, that holds an Array of all the items, which have already been created.
That helper class needs to add itself as a listener to all the items for the event, that you start drag an item.
Once an item is dragged, the helper class adds itself as a listener to the mouse move event of that item
in the mouse move event handler, the helper class does a hit test from the currently dragged item to each item in its list of items (simple for loop)
if a hit test results with true, you save item you have currently tested against, as this is the one, that you dragged item is overlapping with.
Now you can do measurements (comparing positions, bounding boxes of the two items, etc) to figure out, where to position your dragged item, so that it does not overlap anymore.
Don't forget to remove the mouse move event listener in the event of the user ending to drag.
A few weeks ago I was working on a project wich had very specific dragging, so we've decided not to use the Flex DragManager. Our, so called, DragManager is not doing anything special. The thing that I think is related with your work is the presence of snap blocks. In our case the snap blocks were simple groups. Every snap block has a reference to the object wich was dropped into it(let's say it is droppedItem). When trying to drop an item, or even before triggering the snapping mechanism, there is a simple check in the snap block
if (droppedItem == null)
And this was the solution in our case.
I'm not sure what are your snapping slots, but I hope that our approach will help you.
Best regards, Ivan

Dragging an object along a drawn path

Same as the question:
I need to drag a component along a programmatically drawn path composed by different kinds of graphic, like lines, curves, etc.
I've already googled it, but with no success.
Any ideas? thanks!
The following is say for a linearly curved path drawn by you. You can use similar method for any direction.
Add an Event listener for click.
(That starts drag)
Track the user's mouse along x
direction for example.
Keep plotting the component's x & y as
Mouse x changes with respect to the
drawn path's x.
Stop relocating as user leaves the
mouse
Start with this if possible & be back with code if you get doubts.
If your drawing part is complete then You can use two dimensional ByteArray. The size of the ByteArray will be the size of your stage, this two-dimensional array will be set to zero, means all your stage locations are set to zero. When any curve or line is drawn , set those locations to one. Now you know at-least where your object can move, the valid locations are those which are set to one.
Now the second the part is how to move an object on the valid path or location using mouse or keyboard.
You will be using Event.EnterFrame for smooth and fast movement of the object,
1--using keyboard.
use up key to move object to upper location if that position or location is set to one else the object will not move Up, same for others.
2-- using mouse move event, detect MouseY position for moving UP or DOWN w.r.t current position of MouseY , and move it respectively if location is set to one.
Hope, this will guide you in right direction...
Will this work? http://www.pixeldigest.com/followthepath.html