Clicking Objects in AS3 - actionscript-3

I'm new to Actionscript 3 and I'm trying to learn as much as I can.
I want to know how to move objects by clicking.
For Example: I will first click on object to select it then I will click on another object(not in the same location) to place the first one on top of it.
*Sorry if my explanation is bad
I don't have the code to show you because I don't know how to do it.
But the way that I thought of is:
- I will click on the first object to select it and that object will go to an array
- That array will have the function of the second click which will transfer the object on top of another one.
The problem is that there is chance that i can select two objects on the first one (It should be only one). Is there any way to fix this? Or is there a way to make this easier?

Related

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.

How To Merge Objects In AS3, So That They Become Draggable as a Whole

I'm quite new to as3, and I'm heading with a (for me complex) problem.
I have some identical objects (movieclips), wich I made draggable sqaures.
But I want to achieve the following thing:
When I drag one of these objects to another one, I want them to "attach" to each other, just like in some "building games".
And after two (or more) objects are attached, I need them to be draggable as a new whole.
Which means if you click on any of them, every object drags along, en they stay om the same attached position.
I honestly don't know how to do this.
I would appreciate if anyone could provide some examples or code.
Thanksin advance!
Here's a simple algorithm for you:
1) Make an array that will hold "draggable" objects.
2) Upon selecting the first object add it to the array.
3) On FRAME_ENTER move (translate) all the object that are in the draggable array and check objects in the array collision with other objects. If the collision is true, add those objects to the draggable item array.
People here won't write code for you, it's up to you. But if you run into a problem where your code does not work how you intend it, feel free to post it and surely some help will come!

Draw letters based on points on mouse click in action script 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.

Can two buttons with the same class instance act differently in Flash?

Say I have this specific button class in Flash called cont_button and it's supposed to be used to break out of a loop, but I want to use the class more than once. Is there a way to give every instance of this class some kind of parameter so that it knows which frame it nees to go to?
Example:
I have an instance of cont_button on frame 200 and there's a loop between 200 and 210. This cont_button executes a gotoAndPlay(211). But later on I have another instance of the button on frame 315 and a loop between 315 and 325. Is there a way to make it so each instance knows which frame it specifically needs to go to via the use of a variable? Or am I going to have to make an actionscript file for each individual one?
Pretty new to ActionScript so I appreciate the help and if there are good coding references to AS3 you guys recommend, I'll gladly look those over.
Sure, this is possible. One way you can do this is make the frame numbers class variables and when the button is clicked, they reference whatever value is stored in them, rather then hardcoded numbers. To get a better idea, can you post the relevant parts of your button class?
As you say, you need to pass a parameter to each instance of the button. There are lots of different ways you could do this, but I'd be tempted to just do it via the instance names.
You could name each button loopBreakTo211, loopBreakTo326 and so on, then in your button's class have:
var breakFrame:Number = Number(name.replace("loopBreakTo", ""));
(parent as MovieClip).gotoAndPlay(breakFrame);
Admittedly that's not a very robust way of doing it (for example, it will break if a button is named incorrectly and breakFrame ends up as NaN, so you might want to add a check for that), but it keeps the parameter together with the instance instead of in the timeline somewhere.

Cocos2d: Calling function from one CCLayer in another (parent?) CCLayer

Hello awesome dev community.
I have looked for an answer to this for days now and just can't seem to find a handle, so decided to post Q myself.
I have a game that has menus for picking items.
The menus can have 2 -> 30+ items, so they need to scroll. There are also category menus, which when an item is clicked, a new menu with that category's items appears.
I figured that the most efficient way to go about it is by creating all the elements needed for the menu on a dedicated layer.
The issue is that I have no idea how to call my addNewItems:itemsArray function defined in the main game scene, from the CCLayer containing the menu.
Or, should I just use one layer? A bit messy and difficult to move multiple items together.
Thank you so much for any help - or pointing me in the direction of a clear tutorial or examples of how to do that since I honestly couldn't find any.
Below is a flowchart of what I'm looking to create.
Thanks!!
Hanaan
Finally got an answer from David994A (cocos2d forum)
The answer is to pass the layer a pointer to it's parent layer
I initialize the CCLayer like this:
-(id) initWithParent:(CCLayer *)parentLayer;
and call it like this:
ItemsMenuLayer *tempLayer = [[ItemsMenuLayer alloc] initWithParent:self];
Then, from inside the CClayer I can call any of the parent's layer functions, like this:
[parentLayer functionName];