Copy link to clipboard
Copied
I'm making a game in AS3.
I've got childs, named emptyspace, added to my stage in my Engine.as like that :
emptyspace= new EmptySpace(stage, usableItems[0]);
addChild(emptyspace);
emptyspace2= new EmptySpace2(stage, usableItems[0]);
addChildAt(emptyspace2, 0);
Each emptyspace got is own class (EmptySpace.as and EmptySpace2.as).
When I click on an emptyspace, a useBox appears (UseBox.as)
Is it possible to save "in memory" wich emptyspace was clicked but use it later ?
Because, then, the player choose in the useBox if he wants to build or not.
If he clicks on "build", a window opens with different buildings (Building.as).
Now, I was wondering if it's possible that, when he clicks on "Library" for exemple, the Library appears on the emptyspace wich was clicked before ("the saved click") ?
For the moment, I've got this code :
When he clicks on "Library" in the Building window it' calling this function :
private function poulaillerConstruction(e:MouseEvent😞void{
Engine.library.buildIt(e);
Engine.building.visible = false;
}
In the Library.as :
public function buildIt(e:MouseEvent😞void{
stageRef.addChild(this);
this.visible = true; }
So, for the moment, it appears anywhere on the screen (always at the same place of course) and not where the emptyspace was.
(And if I click on the second empty space, the Library appears at the same place that the first...Don't want that either).
Any idea ?
Thank you very much,
Copy link to clipboard
Copied
YOu should be able to create a variable that you use to store which emptyspace was last selected. YOu could name it something like currentSpace, and when you click an emptyspace you assign that emptyspace to the currentSpace variable. Then you could have your buildIt function target the currentSpace location for locating where to plant the object.
Copy link to clipboard
Copied
EDIT
So, I've put in my Engine.as
emptyspace.addEventListener(MouseEvent.CLICK, onEmptySpaceClicked);
private function onEmptySpaceClicked(e:MouseEvent):void{
var myVar = e.currentTarget;
if (e.currentTarget.name == emptyspace){
//Don't know what to put }
if (e.currentTarget.name == emptyspace2){
}
}
So now, how can I do to put in my buildIt function in Tower.as "Listen the e.currentTarget.name ?
Something like this ? :
public function buildIt(e:MouseEvent):void{
if (e.currentTarget.name == emptyspace){
//Build here }
if (e.currentTarget.name == emptyspace2){
//Build here }
}
And what is the code to tell "Build here ON the emptyspace clicked" ?
Thank you for your help,
Copy link to clipboard
Copied
you have to be careful when you create objects via code.
import flash.display.MovieClip;
var mc:MovieClip = new MovieClip();
trace(mc.name);
//this will not result in "mc" but in "instance1"
//because Flash declares a unique indetifier if you don`t do it yourself
//you could always declare it like this: mc.name = "mc1";
//this will overwrite the identifier Flash provided
so your condition
if (e.currentTarget.name == emptyspace)
will never be true
Copy link to clipboard
Copied
I've put
trace(e.currentTarget);
trace(myVar);
trace(e.currentTarget.name);
In my onEmptySpaceClicked function and I've got, as a result :
[object Emptyspace]
[object Emptyspace]
instance154
So
if (e.currentTarget.name == instance154)
could be true ?
Copy link to clipboard
Copied
tthe property name needs a String
if (e.currentTarget.name == "instance154")
but it would be better if you simply name your objects when you create them:
emptyspace= new EmptySpace(stage, usableItems[0]);
emptyspace.name = "ES1";
emptyspace= new EmptySpace2(stage, usableItems[0]);
emptyspace2.name = "ES2";
Copy link to clipboard
Copied
Thanks ! I've changed the name.
And do you know how I could tell
if (e.currentTarget.name == "ES1"){
Place the movieClip "tower" on it
}
if (e.currentTarget.name == "ES2"){
Place the movieClip "tower" on it
}
?
Copy link to clipboard
Copied
it depends whether you created the movieClip via Code or if it is already sitting on the stage:
if you made it with code like so:
var tower:Tower = new Tower();
addChild(tower)
....
you can simply
state:
if (e.currentTarget.name == "ES1"){
tower.x =e.currentTarget
.x;
tower.y =
e.currentTarget
.y;
}
Copy link to clipboard
Copied
Thanks. Yes, everything is created by code;
Is it possible to use the currentTarget info later ?
Exemple :
1. The player click on an empty space --> the code remember wich empty space was clicked (Engine.as)
2. A window open where the player can choose the building he wants (Building.as)
3. the player clicks on the tower, the window disappear and the tower is placed on the emptyspace wich was clicked (Tower.as)
So far I've got everything, except the code that place the tower on the clicked emptyspace.
So I've got that in my Building.as
tower.addEventListener(MouseEvent.CLICK, towerConstruction, false, 0, true);
Engine.tower.buildIt(e);
Engine.batiments.visible = false;
So, if I follow your advice,I must have in my Tower.as that :
public function buildIt(e:MouseEvent):void{
if (e.currentTarget.name == "ES1"){
this.x = e.currentTarget.x;
this.y = e.currentTarget.y; }
Will it work ?
I suppose not, because it doesn't know which emptyspace was clicked..
Thank again for your advice,
Copy link to clipboard
Copied
Maybe I should use dispacthEvent.
What do you think ?
DispatchEvent when an emptyspace is clicked and an EventListener in the buildIt function.
Copy link to clipboard
Copied
DispatchEvent when an emptyspace is clicked and an EventListener in the buildIt function.
This
Copy link to clipboard
Copied
Engine.as
private function onEmptySpaceClicked(e:MouseEvent):void{
trace("click on it neww");
var myVar = e.currentTarget.name;
if (e.currentTarget.name == "ES1"){
trace("il faut écouter emplacement");
stage.dispatchEvent(new Event("ES1 clicked"));
}
if (e.currentTarget.name == "ES2"){
trace("il faut écouter emplacement VARI");
stage.dispatchEvent(new Event("ES2 clicked"));
}
Tower.as
public function buildIt(e:MouseEvent):void{
stage.addEventListener("ES1 clicked", showBuilt, false, 0, true);
stage.addEventListener("ES2 clicked", showBuilt2, false, 0, true);
}
public function showBuilt():void{
this.x = e.currentTarget.x;
this.y = e.currentTarget.y;
trace("placed at the exact same point");
}
public function showBuilt2():void{
this.x = e.currentTarget.x;
this.y = e.currentTarget.y;
trace("placed at the exact same point2");
It doesn't work. Did I make a mistake ?
Copy link to clipboard
Copied
you have to first understand the concept of eventlisteners (read up on it in the API)
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e4e.html
stage.addEventListener("ES1 clicked", showBuilt, false, 0, true);
public function showBuilt():void{
this.x = e.currentTarget.x;
this.y = e.currentTarget.y;
trace("placed at the exact same point");
}
is just plain wrong in so many ways.
Why did you attach this to stage.
What kind of Event is a string, anyway.
the target of an EventListener is the object it was attached to so
so
this.x = stage.x //not what you want
It doesn't work.
is no valid information. Didn`t it compile? Were there errors? If yes, where, how many?
In any case: take a step back and try to understand the basic concepts of oop first, or try to modify existing code to understand the inner workings.
Its no use to just randomly change sth in your code, hoping for the best.
This project might be out of your skillrange at the moment, try first some smaller projects.
All the Best
Copy link to clipboard
Copied
Thanks.
Ok, so i found another way.
All this are in my Engine.as
private function onEmptySpaceClicked(e:MouseEvent):void{
var myVar = e.currentTarget.name;
if (e.currentTarget.name == "ES1"){
trace("click on one");
clickPuzzles.click1 = true;
}
if (e.currentTarget.name == "ES2"){
trace("click on two");
clickPuzzles.click2 = true;
}
public function buildIt(e:MouseEvent):void{
if (clickPuzzles.click1){
test.x = e.currentTarget.x;
test.y = e.currentTarget.y;
test.visible = true;
}
}
The buiIIt function is called by an other class.
Everything is working, but, is it possible to save e.currentTarget.x and y on the "onEmptySpaceClicked" function as a value ?
And then use it in my buildIt function ?
Like :
private function onEmptySpaceClicked(e:MouseEvent):void{
var myVar = e.currentTarget.name;
if (e.currentTarget.name == "ES1"){
trace("click on one");
clickPuzzles.click1 = true
e.currentTarget.x && e.currentTarget.y = valueOfEs1
}
if (e.currentTarget.name == "ES2"){
trace("click on two");
clickPuzzles.click2 = true
e.currentTarget.x && e.currentTarget.y = valueOfEs2
}
public function buildIt(e:MouseEvent):void{
if (clickPuzzles.click1){
test.position = valueOfEs1
test.visible = true;
}
if (clickPuzzles.click2){
test.position = valueOfEs2
test.visible = true;
}
}
It is, of course, not a real code that I used but it is just for showing you the principle.
Thank you again
Copy link to clipboard
Copied
Hmmm it won't work as it can't know what is "currentTarget" in
this.x = e.currentTarget.x; in my Tower.as
Got to find something else