I am trying to create a system that displays the name of the button that you press.
The button names are put into an array, however it only recognized the last item entered into the array.
Help would be greatly appreciated.
var items:Array = [a, b, c]; //The name of each button
for each(var index in items)
{
index.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
function mouseClickHandler(event:MouseEvent):void
{
trace(index.name); //Should display the name of any of the buttons clicked.
}
You should trace the currentTarget name:
var items:Array = [a, b, c]; //The name of each button
for each(var index in items) {
index.addEventListener(MouseEvent.CLICK, mouseClickHandler);
}
function mouseClickHandler(event:MouseEvent):void {
trace(event.currentTarget.name); //Should display the name of any of the buttons clicked.
}
There's only one index variable created here - and mouseClickHandler function, obviously, works with its current value only. If you need to refer to specific values (given at each loop step), you need to localize them in one way or another:
function generateClickHandler(index:someType) {
return function(event:MouseEvent):void { trace(index.name); }
}
...
for each(var index in items)
{
index.addEventListener(MouseEvent.CLICK, generateClickHandler(index);
}
I'd suggest checking this thread as well.
Related
I need after call a function, set to empty an array and immediately add a new one to there.
This is becouse when an #click event is called i need to call a dialog and populate the content of this with a dynamic component (called them with a slug propertie), so the array should be change accordily to pass the slug propertie to the component.
My code is:
slugs: []
slugConversacion(slug) {
if (this.slugs > 0) {
this.slugs = []
// this.slugs.splice(this.slugs.indexOf(slug), 0);
// this.$delete(this.slugs, this.slugs.indexOf(slug))
}
else {
this.slugs.push(slug);
}
}
<Conversacion
v-for="slug in slugs"
:key="slug.id"
:slug="slug.slug"
></Conversacion>
This not work because when i click the event slugConversacion() set and empty array and only when clicked again, populate. I think that is for the if/else conditional.
What would be the right approach ? Thanks!
Just do this:
if (this.slugs.length > 0) {
this.slugs = [];
}
this.slugs.push(slug);
If you have trouble with setting array to empty with assigning [] to array and loosing reactivity, then you can try next thing.
This works for me as the last resort:
var i = slugs.length;
while(i --){
slugs.splice(i, 1);
}
You should do splice in revers mode because of index confusion: every time will be remained array element with index 1 if you will go throw the loop via
slugs.forEach((item, index) => {
slugs.splice(index, 1);
});
And after that you can do:
this.slugs.push(slug);
From what my friend has told me, this should be working but it is not.
var P2hb:Array = new Array(P2char1, P2char2, P2char3);
var P2life:Number = 0;
addEventListener(Event.ENTER_FRAME, framecheck)
function framecheck(event:Event):void
{
if (P2hb.hitTestObject(P1attack)) { P2life-=2; }
}
This is a generic code but it is the same as what I have. Basically,
all elements in the P2hb are movieclips on the stage.
I want to say that, if P1attack hits any of the objects in the array, then P2life will drop by 2, without having to type hitTestObject() for each individual object.
I can't seem to get it to work. Can anyone tell me what I am doing wrong?
Thank you in advance.
Simply, loop through each individual "movieClip" in the array (using a for loop, for example) and check for the collision against your other object:
function framecheck(event:Event):void
{
for each (var enemy in P2hb) {
if (enemy.hitTestObject(P1attack)) {
P2life-=2;
trace("hit occurred! P2life: "+P2life);
}
}
}
So, I am trying to check the existence of recently pushed array object.
I am developing the web application using angularJS.
I have an array which is defined as
vm.data.detail
So I have a form that let user to CRUD a module. The module is about product stock take. There is a button that will run addProduct() function in my angular controller.
addProduct function:
function addProduct() {
//to check whether the array is empty.
if (vm.data.detail.length == 0) {
vm.data.detail.push({
product: vm.data.product_id.selected,
current_qty: vm.data.product_id.selected.qty,
new_qty: Number(vm.data.product_id.selected.qty) - Number(1),
difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)),
remarks: ''
});
console.log("Product just been added");
}
//if the array is not empty
else {
for (var i = 0; i < vm.data.detail.length; i++) {
//to check whether the selected product is already inside the array
if (vm.data.product_id.selected.name == vm.data.detail[i].product.name) {
console.log("same product selected");
//data
}
//if there is no selected product inside the array, then add it
else {
console.log("different product has just been selected");
vm.data.detail.push({
product: vm.data.product_id.selected,
current_qty: vm.data.product_id.selected.qty,
new_qty: 0,
difference: 0,
remarks: ''
});
}
}
}
}
Above code works well when the array just consists of one product. The problem occurs when I am trying to add another product B into the product. Here is the condition :
Product A is already inside the array.
Product B is selected, and then added into the array. Now the array consists of 2 products.
When I am testing to add a new product B, I don't know why the array is still pushed with a new Product B. So now, the array consists of 3 Products (1 Product A, and 2 Product B).
What I wanted is, when I am trying to add the second product B, the array won't be pushed by new product B.
What am I missing here? Have been dealing with it for hours and can't figure out what I have to add for the "validation".
Please note that, the object the array pushing is already correct. I am just don't know how to put the if else. Looks like the logic inside is still lacking of something, but I couldn't figure out what is missing
Thank you so much for the help given.
You're commiting a simple mistake, you are doing the check of else inside the array, you must move it to outside.
Tips:
You can use Array.prototype.find() method to check if the element exists in array, which is much better than perform a traditional for-loop.
As you may have noticed in documentation, the find method have no compatibility with IE and Opera browsers, so if you need this compatibility, you can use Array.prototype.filter().
Below is the code, with both versions (with find and filter) and also with the necessary modifications:
function addProduct() {
//to check whether the array is empty.
if (!vm.data.detail.length) { // you can simply use (!length) instead of comparing with 0
vm.data.detail.push({
product: vm.data.product_id.selected,
current_qty: vm.data.product_id.selected.qty,
new_qty: Number(vm.data.product_id.selected.qty) - Number(1),
difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)),
remarks: ''
});
console.log("Product just been added");
}
//if the array is not empty
else {
// if there's no obj with the same name inside the array, it returns undefined, otherwise it returns the object.
var obj = vm.data.detail.find(function(value) {
return value.product.name == vm.data.product_id.selected.name;
});
/* Using FILTER:
var obj = vm.data.detail.filter(function(value) {
return value.product.name == vm.data.product_id.selected.name;
})[0];
*/
// Now you can test, if the object exists
if (obj) {
console.log("same product selected");
}
//if there is no selected product inside the array, then add it
else {
console.log("different product has just been selected");
vm.data.detail.push({
product: vm.data.product_id.selected,
current_qty: vm.data.product_id.selected.qty,
new_qty: 0,
difference: 0,
remarks: ''
});
}
}
}
I hope it helps!
It's a basic logic error. You're doing
for each element {
if element is different from given {
add given to array
}
}
What you need to do is
var allElementsDifferentFromGiven = true
for each element {
if element is same as given {
allElementsDifferentFromGiven = false
break
}
}
if (allElementsDifferentFromGiven) {
add given to array
}
But JavaScript arrays have methods to do that:
if (array.every(function(element) {
return true if element is different given
})) {
add given to array
}
I think problem is here :
because there product is property on Detail object and product does not have name property , it is not matching criteria and going to else condition and pushing into name list.
//to check whether the selected product is already inside the array
if(vm.data.product_id.selected.name == vm.data.detail[i].product.name){
it should be
//to check whether the selected product is already inside the array
if(vm.data.product_id.selected.name == vm.data.detail[i].product){
console.log("same product selected");
//data
}
i've created an array. Each element is a button object. Is there a possibility to hook mouseclick on every array at the same time? I mean something like this.
var Objects:Array = new Array
Objects[0] = new button(parameters)
Objects[1] = new button(parameters)
Objects[2] = new button(parameters)
Objects[n].addEventListener(MouseEvent.CLICK, Clicked(n));
function Clicked(n,...)
{
THECODE PROCEEEEDS for Objects[n]
}
I know that's not the clearest and most correct writing, but I'm asking if this is possible in similiar way? And how to do it? I know I can hook every mouseclick and then check if the clicked under the mouse is one of the array elements with for loop, but I'm asking about this way.
Yes. You are unable to directly pass an index into a listener, but you can retrieve that via calling indexOf() inside it.
for each (b in Objects) b.addEventListener(MouseEvent.CLICK, clicked);
// note, you just put function name here!
public function clicked(e:MouseEent):void {
var i:int=Object.indexOf(e.target);
if (i==-1) {
// panic behavior
return;
}
// now you can parse that index into something valuable
}
So I have this Array, named combo. I want to fill it with movieclips according to the user clicks (if user clicks 1, then add "1" to the Array and display it on an area.
Problem is, I do not know how to do it.
For the record, I have 4 movieclips, fire, wind, earth and spirit. When the user clicks one of them, I want the Array to update. Array's max length is 6 and it is always checked against a "true_combo" Array.
My questions are:
1) How do I remove the oldest item in the Array if the length has reached 6?
2) How do I display the movieclips one next to another (smaller versions of the buttons, not new movieclips), according to Combo?
3) How do I dynamically update the movieclip list on the stage?
This is a stripped version of my current fla
https://www.dropbox.com/s/17d9rgclz29ft1u/Untitled-1.fla
This is the code to check the combo against true combo:
function checkthis(e:Event)
{
if(combo.length == truecombo.length)
for(var o:int = 0; o < combo.length; o++)
{
if(combo[o] == truecombo[o])
{
bravo.play();
}
}
}
1
You can use the Array.shift() (remove first item in array) or Array.pop() (remove last item in array)
2 / 3
Use a for-loop and change the position of the items according the array. I assume the items in the array are references to the movieclips, then this would be functions you could use.
function add(clip:DisplayObject)
{
if (this.combo.length >= 6)
{
// remove child and remove it from the list
removeChild(this.combo.shift());
}
this.combo.push(clip);
this.addChild(clip);
reorder();
}
function reorder()
{
for(var i:int = 0; i < combo.length; i++)
{
combo[i].x = i * 50;
}
}
update:
You can download an example of this implementation here (Flash CS6 file) or watch it here
update 2
function checkthis(e:Event)
{
var isValid:Boolean = true;
if(combo.length == truecombo.length)
{
for(var o:int = 0; o < combo.length; o++)
{
if(combo[o] != truecombo[o])
{
// if one item isnt the same it should fail.
isValid = false;
}
}
}
if (isValid)
{
bravo.play();
}
}