How to get menuitem array's index? - cocos2d-js

I want to get an index of menuItem.
How can I get it in the menuItem function?
var button=[];
var menu;
for(var i=0;i<3;i++){
button[i]=new cc.MenuItemImage(
res.ball_png,
res.ball_png,
function(){
cc.log(i); // this part
},this);
button[i].setTag(i);
button[i].x=100*i;
}
menu=new cc.Menu(button);
this.addChild(menu);

I found myself.
var button=[];
var menu;
for(var i=0;i<3;i++){
button[i]=new cc.MenuItemImage(
res.ball_png,
res.ball_png,
function(val){
cc.log(val.getTag());
},this);
button[i].setTag(i);
button[i].x=100*i;
}
menu=new cc.Menu(button);
this.addChild(menu);

Related

How to Loop through a list of elements

My question is very simple, how can i create a loop that will loop a simple list of elements.
List li=["-","\\","|","/"];
this is my dart list and i want to create this simple animation.
Different ways to Loop through a List of elements
1 classic For
for (var i = 0; i < li.length; i++) {
// TO DO
var currentElement = li[i];
}
2 Enhanced For loop
for(final e in li){
//
var currentElement = e;
}
Notice the keyword final. It means single-assignment, a final variable's value cannot be changed.
3 while loop
var i = 0;
while(i < li.length){
var currentElement = li[i];
i++;
}
For the while loop, you will use var to reassign the variable value.
Try this code to loop through a list:
List li=["-","\\","|","/"];
for (var i=0; i<li.length; i++) {
print(li[i]);
}
As to the animation:
HTML
<p id="test">
test
</p>
Dart
import 'dart:html';
import 'dart:async';
main() async {
List li = ["-", "\\", "|", "/"];
for (var i = 0; i < 400000000; i++) {
querySelector('#test').text = li[i % 4];
(await new Future.delayed(const Duration(seconds: 1)));
}
}
Another ways of looping through a List in Dart:
Using the forEach method:
li.forEach((value) {
var currentElement = value;
});
Using a While Loop and an Iterator:
// First, get an iterator to the list:
var myListIter = li.iterator;
// Iterate over the list:
while(myListIter.moveNext()){
var currentElement = myListIter.current;
}
there are may methods to get a loop from list
for example we have this type of list
var mylist = [6, 7 ,9 ,8];
using for each method
mylist.forEach((e){
print(e);
});
using for Loop
for (int i=0 ; i<mylist.length; i++){
var e = mylist[i];
print(e);
}
Enhanced For loop
for (var e in mylist){
print(e);
}
using while loop
var i = 0;
while(i < mylist.length){
print(mylist[i]);
i++;
}

Programmatically create polymer Elements in Dart

Need add polymer paper-dropdown-menu in DOM.
I try this code:
makePapersElements() {
List _items = new List();
for (var i = 0; i < 13; i++) {
PaperItem item = new dom.Element.tag('paper-item');
item.text = i;
_items.add(item);
}
return _items;
}
And add nodes in PaperListbox then in PaperDropdownMenu:
List<PaperItem> items = makePapersElements();
var element = new dom.Element.tag('div');
PaperDropdownMenu dropMenu = new PaperDropdownMenu();
PaperListbox listBox = new dom.Element.tag('paper-listbox');
dropMenu.append(listBox);
listBox.nodes.addAll(items);
element.nodes.add(dropMenu);
$['example'].nodes.add(element);
It's not work currently:
How it can be done?
Update: Added Gist
https://gist.github.com/Rasarts/a0b6710e234ec8b4aa37f90e4cd14839
You can create PaperDropDownMenu and Paperlistbox with new Xxx(), no need for new dom.Element.tag('Xxx') because these elements contain a constructor for convenience where this is done already
https://github.com/dart-lang/polymer_elements/blob/7912e0e6641155505007a89140f11c25db14e3f8/lib/paper_listbox.dart#L61
https://github.com/dart-lang/polymer_elements/blob/7912e0e6641155505007a89140f11c25db14e3f8/lib/paper_dropdown_menu.dart#L69
I guess the issue is because you don't use the Polymer DOM API. See also https://github.com/dart-lang/polymer-dart/wiki/local-dom. Only when you enable full shadow DOM (with full polyfills whithout native support) then you don't need to use this API.
makePapersElements() {
List _items = new List();
for (var i = 0; i < 13; i++) {
PaperItem item = new PaperItem();
item.text = i;
_items.add(item);
}
return _items;
}
List<PaperItem> items = makePapersElements();
var element = new dom.Element.tag('div');
PaperDropdownMenu dropMenu = new PaperDropdownMenu();
PaperListbox listBox = new PaperListbox();
Polymer.dom(dropMenu).append(listBox);
// not sure this will work
Polymer.dom(listBox).childNodes.addAll(items);
// alternatively
var listboxDom = Polymer.dom(listBox);
for(var item in items) {
listboxDom.append(item);
}
Polymer.dom(this)appen(dropMenu);
// ro Polymer.dom(this.root)appen(dropMenu);
Polymer.dom($['example']).append(element);
Yes, I do it wrong. Example helped. Thanks.
https://github.com/bwu-dart-playground/polymer1/blob/12a4bca9c5c5b21c690af0bd4451407b64899a6e/so36689312_programmatically_create_paper_elements/web/pdm_element.dart#L36-L36

what is the best technique for delete entities (many to many) breeze

customers is data.results from query (ids is guid)
selectedIds is an array from guid.
function selectedMap(selectedIds) {
var items = [];
for (var i = 0; i < selectedIds.length; i++) {
var match = ko.utils.arrayFirst(customers, function (item) { //-
return selectedIds[i] === item.id();
// or item.entityAspect.setDeteted(); if true
});
items.push(match);
}
return items;
Is there something better?
breeze 1.5.1
var countries = ['Austria', 'Italy', 'Norway']
var query = EntityQuery.from("Customers")
.where("country", 'in', countries);

Check if textboxes have the same content

I want to check if the textboxes created like this:
(function(arr) {
for (var i = 0; i < arr.length; i++) {
app.add(app.createLabel(arr[i] + " mail"));
app.add(app.createTextBox().setName(arr[i]));
}
})(["first", "second", "third"]);
have the same contents? I was looking for something like getElementByTagname or or getTextboxes, but there are no such functions.
So how to iterate thvrough them and show a label if they are all equal?
To access any widget values you need to add them as a callback element (or a parent panel) to the server handler that will process them. The values of each widget are populated on a parameter passed to the handler function and can be referenced by the widget name (that you already set).
You don't need to setId as suggested on another answer. Unless you want to do something with the widget itself (and not its value). e.g. change its text or hide it, etc.
var textBoxes = ["first", "second", "third"];
function example() {
var app = UiApp.createApplication().setTitle('Test');
var panel = app.createVerticalPanel();
textBoxes.forEach(function(name){
panel.add(app.createLabel(name + " mail"));
panel.add(app.createTextBox().setName(name));
});
panel.add(app.createLabel('Example Label').setId('label').setVisible(false));
var handler = app.createServerHandler('btnHandler').addCallbackElement(panel);
panel.add(app.createButton('Click').addClickHandler(handler));
SpreadsheetApp.getActive().show(app.add(panel));
}
function btnHandler(e) {
var app = UiApp.getActiveApplication(),
allEqual = true;
for( var i = 1; i < textBoxes.length; ++i )
if( e.parameter[textBoxes[i-1]] !== e.parameter[textBoxes[i]] ) {
allEqual = false; break;
}
app.getElementById('label').setVisible(allEqual);
return app;
}
Notice that ServerHandlers do not run instantly, so it may take a few seconds for the label to show or hide after you click the button.
When you create the textboxes, assign each one an id using setId(id).
When you want to obtain their reference later, you can then use getElementById(id).
Here is an example:
function doGet() {
var app = UiApp.createApplication();
app.add(app.createTextBox().setId("tb1").setText("the original text"));
app.add(app.createButton().setText("Change textbox").addClickHandler(app.createServerHandler("myHandler")));
return app;
}
function myHandler() {
var app = UiApp.getActiveApplication();
app.getElementById("tb1").setText("new text: in handler");
return app;
}

Can flash.display.Bitmap have events on themself?

Is this possible or need I do it anyway else? The problem is that the event is not responding.
[Embed(source="pic1.jpg")]
private var Img1:Class;
var i1:Bitmap = new Img1();
// not working
i1.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
t.htmlText = "Click!";
});
As you can see Here, Bitmap is not descendant of InteractiveObject. Only interactive objects can be part of an input processes of Flash.
To do what you want encapsulate the Bitmap with Sprite:
[Embed(source="pic1.jpg")]
private var Img1:Class;
var i1:Bitmap = new Img1();
var s1:Sprite = new Sprite();
s1.addChild(i1);
s1.addEventListener(MouseEvent.CLICK, function(e:MouseEvent) {
t.htmlText = "Click!";
});

Resources