discord.js command handling else if problem - discord.js

I'm new to discord.js and I'm trying to make a really simple command with command handling.
The content of the bot.js file:
const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token } = require("./config.json");
const fs = require("fs");
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('On!');})
client.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
} catch (error) {
console.error(error);
message.reply("Error");
}
The content of the lunedi1.js file:
const Discord = require('discord.js');
const fs = require("fs");
const client = new Discord.Client();
const { prefix, token } = require("../config.json");
module.exports = {
name: "lunedi1",
description: "Codici videolezioni, li puoi ottenere facendo '.<giorno> <ora>",
execute(message, args) {
if(message.content.startsWith(prefix + "lunedi1")){
return message.channel.send("Diritto, 1630027401")
}else if(message.content.startsWith(prefix + "lunedi2")){
return message.channel.send("Italiano, 959924441")
}else if(message.content.startsWith(prefix + "lunedi3")){
return message.channel.send("Italiano, 959924441")
}else if(message.content.startsWith(prefix + "lunedi4")){
return message.channel.send("Fisica LAB, 954071647" )
}else if(message.content.startsWith(prefix + "lunedi5")){
return message.channel.send("Informatica, 1213333512")
}else if(message.content.startsWith(prefix + "martedi1")){
return message.channel.send("Informatica, 1213333512")
}else if(message.content.startsWith(prefix + "martedi2")){
return message.channel.send("Georgrafia, 951609580")
}else if(message.content.startsWith(prefix + "martedi3")){
return message.channel.send("Chimica, 952232255")
}else if(message.content.startsWith(prefix + "martedi4")){
return message.channel.send("Chimica, 952232255")
}else if(message.content.startsWith(prefix + "martedi5")){
return message.channel.send("Inglese, 953894058")
}else if(message.content.startsWith(prefix + "martedi6")){
return message.channel.send("Storia, 959924441")
}else if(message.content.startsWith(prefix + "mercoledi1")){
return message.channel.send("Grafica, 954902455")
}else if(message.content.startsWith(prefix + "mercoledi2")){
return message.channel.send("Italiano, 959924441")
}else if(message.content.startsWith(prefix + "mercoledi3")){
return message.channel.send("Italiano, 959924441")
}else if(message.content.startsWith(prefix + "mercoledi4")){
return message.channel.send("Inglese, 953894058")
}else if(message.content.startsWith(prefix + "mercoledi5")){
return message.channel.send("Inglese, 953894058")
}else if(message.content.startsWith(prefix + "mercoledi6")){
return message.channel.send("Religione, 955444970")
}else if(message.content.startsWith(prefix + "giovedi1")){
return message.channel.send("Matematica, 957066907")
}else if(message.content.startsWith(prefix + "giovedi2")){
return message.channel.send("Matematica, 957066907")
}else if(message.content.startsWith(prefix + "giovedi3")){
return message.channel.send("Grafica, 954902455")
}else if(message.content.startsWith(prefix + "giovedi4")){
return message.channel.send("Grafica, 954902455")
}else if(message.content.startsWith(prefix + "giovedi5")){
return message.channel.send("Scienze della Terra, 1632151557")
}else if(message.content.startsWith(prefix + "venerdi1")){
return message.channel.send("Fisica, 1752593012")
}else if(message.content.startsWith(prefix + "venerdi2")){
return message.channel.send("Fisica, 1752593012")
}else if(message.content.startsWith(prefix + "venerdi3")){
return message.channel.send("Chimica, 952232255")
}else if(message.content.startsWith(prefix + "venerdi4")){
return message.channel.send("Storia, 959924441")
}else if(message.content.startsWith(prefix + "venerdi5")){
return message.channel.send("Educazione fisica, 140876521")
}else if(message.content.startsWith(prefix + "venerdi6")){
return message.channel.send("Educazione fisica, 140876521")
}else if(message.content.startsWith(prefix + "sabato1")){
return message.channel.send("Informatica, 1213333512")
}else if(message.content.startsWith(prefix + "sabato2")){
return message.channel.send("Diritto, 1630027401")
}else if(message.content.startsWith(prefix + "sabato3")){
return message.channel.send("Scienze della Terra, 1632151557")
}else if(message.content.startsWith(prefix + "sabato4")){
return message.channel.send("Matematica, 957066907")
}else if(message.content.startsWith(prefix + "sabato5")){
return message.channel.send("Matematica, 957066907")
}
},
};
As you can see it is really simple but when I want that when I do like ".martedi2" I don't get "Georgrafia, 951609580" but it only works with ".lunedi1", others don't work and like do other 30 commands will be rlly confusing. If you need more info I can give that. Thx to everyone that will help me :)

command name is lunedi1 thats why. other will not work because message content starting does not starts with other, you can use arguments to solve this problem
code:
const Discord = require('discord.js');
const client = new Discord.Client();
const { prefix, token } = require("../config.json");
module.exports = {
name: "info",
description: "Codici videolezioni, li puoi ottenere facendo '.<giorno> <ora>",
execute(message, args) {
if (!args[0]) return message.channel.send("please give name");
if (args[0] === "lunedi1") {
return message.channel.send("Diritto, 1630027401")
} else if (args[0] === "lunedi2") {
return message.channel.send("Italiano, 959924441")
} else {
return message.channel.send("?");
};
}
};
usage:
[Prefix]info
example:
!info lunedi1
you can do this for other.
Links:
Guide For Arguments

Related

how to return response string from $http get request

I want to get dynamic string response from the server by sent date of an object.
for (var i = 0; i < notifications.length; i++) {
let tempNotification = { ...notifications[i] };
tempNotification.Time = convertTime(tempNotification.CreationDate);
tempArray.push(tempNotification);
}
This is the convert Function:
function convertTime(date) {
const today = new Date();
const creationDate = new Date(date);
const months = today.getMonth() - creationDate.getMonth();
//if hours / 24*30 - moth
if (months > 0) {
return new Date(date).toLocaleString();
} else {
const sum = today.getDate() - creationDate.getDate();
if (sum > 0) {
return sum + " " + GetSpecificReactConst("notification-days-ago");
} else {
const hours = today.getHours() - creationDate.getHours();
if (hours > 0) {
return sum + " " + GetSpecificReactConst("notification-hours-ago");
} else {
return 1 + " " + GetSpecificReactConst("notification-houts-ago");
}
}
}
}
and the GetSpecificReact... is an $http get request:
function GetSpecificReactConst (text) {
$http.get(AppConfig.apiUrl + 'Translations/GetSpecificReactConst?constKey=' + text + '&lang=' + $location.url().split('/')[1]).
then(function (response) {
});
}
How i can get the response?

I'm having problems with combining these two codes together:

This is what I have tried: It only executes the first one.
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const roleName = message.member.roles.cache.find(r => r.name === "Owner")
if (roleName) {
return message.reply("Pog")
} else {
return message.reply("Sorry, an error occured.")
}
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
client.commands.get('ping').execute(message, args);
} else if (command == 'youtube') {
client.commands.get('youtube').execute(message, args);
} else if (command == 'clear') {
client.commands.get('clear').execute(message, args);
} else if (command == 'mute') {
client.commands.get('mute').execute(message, args);
} else if (command == 'unmute') {
client.commands.get('unmute').execute(message, args);
}
});
I'm new to js and I need help with combining these two codes together.
All I want to do is that the person using command needs to heve certan role, so that like members ...
can't use the mute command.
Yes you can do this, you just want to find the role first and then run an if statement to check if the user has this role before calling .execute(message, args);
This can be done like this:
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'ping') {
client.commands.get('ping').execute(message, args);
} else if (command == 'youtube') {
client.commands.get('youtube').execute(message, args);
} else if (command == 'clear') {
client.commands.get('clear').execute(message, args);
} else if (command == 'mute') {
if (message.member.roles.cache.some(role => role.name == "Owner")) {
client.commands.get('mute').execute(message, args);
} else {
message.reply("You are unable to use this command");
}
} else if (command == 'unmute') {
if (message.member.roles.cache.some(role => role.name == "Owner")) {
client.commands.get('unmute').execute(message, args);
} else {
message.reply("You are unable to use this command");
}
}
});
You should avoid having multiple listeners for the same event and just use one. Also, note that there will be no member object in message if the message was sent in a DM

How to do numeric iterators in angularjs?

I have to convert Java code to angularjs ..In java i used while loop for numeric iterator something like below and in angularjs i tried ng-repeat but its not worked as loop sometime increase by +1 or +2 etc.Can someone please guide ho to do numeric iteration in angularjs?
int curretPos = 0;
String namePattern ="SOmeValue";
while (curretPos < namePattern.length()) {
String featureName = "";
if (Constants.namePatternformat.charAt(0) == namePattern.charAt(curretPos)
&& Constants.namePatternformat.charAt(1) == namePattern.charAt(curretPos + 1)) { // name pattern
curretPos = curretPos + 2;
int nextPos = namePattern.indexOf(Constants.namePatternformat.charAt(2), curretPos);
if (nextPos != -1) {
featureName = namePattern.substring(curretPos, nextPos);
curretPos = nextPos;
if (features.containsKey(featureName)) {
name = name.concat(String.valueOf(features.get(featureName)));
} else {
throw SomeException;
}
} else {
throw SomeException;
}
} else if (Constants.namePatternParent.charAt(0) == namePattern.charAt(curretPos)
&& (Character.toLowerCase(Constants.namePatternParent.charAt(1)) == Character.toLowerCase(namePattern.charAt(curretPos + 1)))) { // parent name pattern
if((namePattern.length() - curretPos) >= Constants.namePatternParent.length()) {
if (!Constants.namePatternParent.equals(namePattern.substring(curretPos, (curretPos+Constants.namePatternParent.length())))) {
throw SomeException;
}
}else{
throw SomeException;
}
curretPos = curretPos + (Constants.namePatternParent.length() - 1);
if(null != parent){
name = name.concat(parent);
}
} else if (Constants.namePatternIncrement.charAt(0) == namePattern.charAt(curretPos)
&& (Character.toLowerCase(Constants.namePatternIncrement.charAt(1)) == Character.toLowerCase(namePattern.charAt(curretPos + 1)))) { // increment name pattern
if((namePattern.length() - curretPos) > Constants.namePatternIncrement.length()) {
if (!Constants.namePatternIncrement.substring(0, 5).equals(namePattern.substring(curretPos, curretPos+5))) {
throw SomeException;
}
}else{
throw SomeException;
}
curretPos = curretPos + (Constants.namePatternIncrement.length() - 2) + 1;
int nextPos =
namePattern.indexOf(Constants.namePatternIncrement
.charAt(Constants.namePatternIncrement.length() - 1), curretPos);
if (nextPos != -1) {
featureName = namePattern.substring(curretPos, nextPos);
curretPos = nextPos;
if (features.containsKey(featureName)) {
if (null != features.get(featureName)
&& features.get(featureName).matches("^-?\\d+$")) {
int currentInstance = Integer.valueOf(features.get(featureName)) + (instance-1);
name = name.concat(String.valueOf(currentInstance));
} else {
throw SomeException;
}
} else {
throw SomeException;
}
} else {
throw SomeException;
}
} else {
name = name.concat(String.valueOf(namePattern.charAt(curretPos)));
}
curretPos = curretPos + 1;
}

Protractor - looping through table to select option from listbox

I'm trying to loop through a table, using Protractor, to see if a row contains a select element. Then select 1 of two particular options. Thus far the test 'passes' but nothing is actually being selected. My latest attempts are below.
it('should loop through table and select an option if listbox is available',
function () {
var table= element(by.xpath("//table[#id='tableID']"));
var count = table.length;
var currentType = "";
for (var i = 1; i <= count; i++) {
tableSelect(i);
}
function tableSelect(i) {
it('should loop through this table and associate', function () {
expect(table.isDisplayed()).toBe(true);
if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]")).isDisplayed()) {
if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]//select[matches(#ng-change,'listChange()')]")).isDisplayed()) {
var varOne = element(by.xpath("//table[#id='tableID']/" +
"tbody/tr[" + i + "]/td[4]/div/span/span")).getText();
if (currentType != "Moretext" || varOne.Length < 6) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i +
"]/td[6]/div/span/div/select")).
element(by.cssContainingText('option',
'This is option A')).click();
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
}
}
else if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed()) {
currentType = element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText();
}
}
});
}
var saveBtn = elem(by.id('saveButton'));
expect(associateBtn.isDisplayed()).toBe(true);
expect(associateBtn.isEnabled()).toBe(true);
saveBtn.click();
});
This is attempt #2:
element.all(by.repeater('row in datarows')).then(function(rows) {
for (var i = 1; i <= rows.length; ++i) {
if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]//select[matches(#ng-change,'listChange()')]")).isDisplayed()) {
var varOne = element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText();
if (currentType != "MoreText" && varOne.Length < 6) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option A')).click();
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
}
}
else if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed()) {
currentType = element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText();
}
}
});
This is the latest attempt:
var varOne = "";
var varTwo = "";
element.all(by.xpath("//table[#id='tableID']/tbody/tr")).then(function (rows) {
for (var i = 1; i < (rows.length); i++) {
console.log('rowcount = ' + i);
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]")).isDisplayed().then(function (visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]//select[#ng-change='listChange()']")).isDisplayed().then(function (visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText().then(function (monthText) {
varTwo = monthText;
console.log(varTwo);
});
if (varOne != "Revolving" || varTwo.length < 6) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'Exclude: Duplicate Account')).click();
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'Include in Ratios')).click();
}
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed().then(function (visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText().then(function (currText) {
varOne = currText;
console.log(varOne);
});
}
});
}
});
}
});
}
});
Code like
if (element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]")).isDisplayed()) {
Won't work because that always evaluate to true since the promise Object is truthy per Javascript booleans.
You need to follow the promise:
var elm = element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]"));
elm.isDisplayed().then(function(visible) {
if (visible) {
// logic here
}
});
Assuming the element is Present, if not present then isDisplayed will fail at webdriver level, so you may test for isPresent instead of isDisplayed
here's what I used to finally get it to work. I put a call to a function inside of a for loop, and inside of the function is the code that does the heavy lifting.
it('should loop through table and select an option from each listbox available', function() {
var varOne = "";
var varTwo = "";
element.all(by.xpath("//table[#id='tableID']/tbody/tr")).then(function (rows) {
for (var i = 1; i < (rows.length); i++) {
console.log('count = ' + i);
selectWithinTable(i);
}
function selectWithinTable(i) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]")).isDisplayed().then(function(visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]//select[#ng-change='listChange()']")).isDisplayed().then(function(visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[4]/div/span/span")).getText().then(function(someText) {
varTwo = someText;
console.log(varTwo);
console.log(varTwo.length);
});
if (varOne != "Revolving" || varTwo === "") {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option A')).click();
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[6]/div/span/div/select")).element(by.cssContainingText('option', 'This is option B')).click();
}
}
else {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).isDisplayed().then(function(visible) {
if (visible) {
element(by.xpath("//table[#id='tableID']/tbody/tr[" + i + "]/td[1]/div/span/span")).getText().then(function (moreText) {
varOne = moreText;
console.log(varOne);
});
}
});
}
});
}
});
}
});
element(by.id('buttonID')).isDisplayed().then(function(visible) {
if(visible) {
element(by.id('buttonID')).isEnabled().then(function(enabled) {
if (enabled) {
element(by.id('buttonID')).click();
}
});
}
});
});
//below code working for me to traverse to the particular rows and cells in the table
async colRowIterate() {
this.rowsTblValue.each(async (rowsValues: any) => {
let cells = rowsValues.$$('td');
cells.get(0).getText().then(async (cellvalues: any) => {
await browser.sleep(10000);
if (cellvalues == 'Harry') {
cells.get(4).$('button').click();
}
});
});

Dynamically removing movieclips

First time poster, long time reader.
I've been having a problem with figuring this out. I've been stuck in my game for 4 hours now, googling and trying to figure out how to do it.
I have a game where i add some cartoonish ants, that when they are clicked, they need to be removed from stage. There are 4 differend kinds of ants, so im doing a Math.random for picking which one to add. (ant 1+2+3 have 50% chance to spawn and 4th 50%)
rnd_nbr = (Math.random() * 5)+1;
I have a timer doing 10 tick, and i reset the timer to make neverending.
Then i have a math random and if sentences adding mc' to the stage with movement from Tweener, and event listeners for clicks.
But i cant figure out how to remove them when clicked.
I have done alot of failed tries right inside the click_candy_anty function.
I've left them commented out.
I apologize for abit messy coding, but it will be cleaned up when(hopefully) it gets working.
Help is highly appreciated.
import caurina.transitions.Tweener;
var ant_index:Array = new Array(10); //index for ants
var ant_number:int;
var ant_temp:int;
var rnd_nbr:int; //var for rnd number
var score:int = 0;
var score_update:String;
var reset_timer:Boolean = false;
var antTimer:Timer = new Timer(800,10); //timer
antTimer.addEventListener(TimerEvent.TIMER, create_ant);
var anty0_:anty_0; //creating ant vars for all 5 kind
var anty1_:anty_1;
var anty2_:anty_2;
var anty3_:anty_3;
var anty4_:anty_4;
var screen_number:int = 0;
var antyArray:Array = new Array(10);
var main:main_mc = new main_mc;
main.x = 0; //0,0
main.y = 0;
addChild(main);
var score_format:TextFormat = new TextFormat();
score_format.size = 25;
score_format.align = TextFormatAlign.CENTER;
var score_txt:TextField = new TextField();
score_txt.defaultTextFormat = score_format;
score_txt.text = "" + score;
score_txt.x = 600;
score_txt.y = 20;
score_txt.border = true;
score_txt.autoSize = TextFieldAutoSize.LEFT ;
score_txt.height = 40;
addChild(score_txt);
var score_txt_update:TextField = new TextField();
score_txt_update.defaultTextFormat = score_format;
score_txt_update.text = "0";
score_txt_update.x = 550;
score_txt_update.y = 20;
score_txt_update.border = true;
score_txt_update.autoSize = TextFieldAutoSize.LEFT ;
score_txt_update.height = 40;
score_txt_update.alpha = 0;
addChild(score_txt_update);
function click_candy_anty(event:MouseEvent):void{
if (score < 20){
//trace("evt: " + evt);
//this.removeChild();
//this.removeChild(this);
//removeChild(evt.currentTarget);
//removeChild(evt.target.name.substr(7));
//removeChild(this);
//removeChild(evt.currentTarget.name);
// trace("The " + evt.target.label + " button was clicked");
// trace(evt.type)
score++;
score_txt.text = "" + score;
//score_update = "+1";
score_txt_update.text = "+1";
//ori position x:570 y:20
score_txt_update.y = -30;
Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.8, transition:"linear", onComplete:score_txt_update_fade});
}
else {
stop_game();
trace("48");
}
}
function score_txt_update_fade(Event = null){
Tweener.addTween(score_txt_update, {y: 50, alpha: 0, time: 0.4, transition:"linear"});
}
function click_leaf_anty(Event = null):void{
if (score > 0 && score < 20){
score--;
score_txt.text = "" + score;
score_txt_update.text = "-1";
score_txt_update.y = 50;
Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.4, transition:"linear", onComplete:score_txt_update_fade_neg});
trace("12 wrong");
}
/* else {
stop_game();
trace("12");
trace("score: " + score + ", ");
}*/
}
function score_txt_update_fade_neg(Event = null){
Tweener.addTween(score_txt_update, {y: -30, alpha: -1, time: 0.8, transition:"linear"});
}
//screen1();
start_timer();
function screen1(Event = null):void {
screen_number = 2;//slet når event listener til scr1 kommer
if (screen_number == 2){
}
else {
screen_number = 2;
}
}
function screen2(Event = null):void {
if (screen_number == 3){
}
else {
screen_number = 3;
}
start_timer();
}
function start_timer(Event = null):void {
if (score < 20) {
if (reset_timer == false){
antTimer.start();
//trace("antTimer initialized");
}
if (reset_timer == true){
antTimer.reset();
antTimer.start();
//trace("antTimer RESETTED & initialized");
}
}
else {
stop_game();
trace("57");
}
}
function stop_game(Event = null):void {
if (screen_number != 2){
screen_number = 2;
trace("Game completed - launching end screen");
// move to next screen
for (var i:Number=0; i<=9;i++){
ant_temp = ant_number;
if (ant_temp < 9) {
//trace("ant_temp er lavere end 9::: " + ant_temp );
ant_temp++;
}
else if (ant_temp == 9) {
//trace("ant_temp lig med 9::: " + ant_temp );
ant_temp = 0;
}
if (ant_index[ant_temp] == 1){
//removeChild(anty1_);
if ("anty1_" + ant_temp != null){
removeChild(getChildByName("anty1_" + (ant_temp)));
}
}
if (ant_index[ant_temp] == 2){
//removeChild(anty2_);
if ("anty2_" + ant_temp != null){
removeChild(getChildByName("anty2_" + (ant_temp)));
}
}
if (ant_index[ant_temp] == 3){
//removeChild(anty3_);
if ("anty3_" + ant_temp != null){
removeChild(getChildByName("anty3_" + (ant_temp)));
}
}
else if (ant_index[ant_temp] == 4){
//removeChild(anty4_);
if ("anty4_" + ant_temp != null){
removeChild(getChildByName("anty4_" + (ant_temp)));
}
}
}//for loop end
screen3();
}
}
function screen3 (Event = null):void{
var end:end_mc = new end_mc;
end.x = 0; //0,0
end.y = 0; //
addChild(end);
}
function create_ant(Event = null):void {
//trace("antTimer triggered");
if (score < 20) {
rnd_nbr = (Math.random() * 5)+1;
ant_index[ant_number] = rnd_nbr;
trace("ant_number" + ant_number);
//trace("ant_index[" + ant_number + "]: " + ant_index[ant_number]);
if (ant_index[ant_number] == 1){
anty1_ = new anty_1();
anty1_.name = "anty1_" + (ant_number);
anty1_.height = 118;
anty1_.width = 102;
anty1_.x = 100;
anty1_.y = 300;
addChild(anty1_);
Tweener.addTween(anty1_, {x: 541, time: 3, transition:"linear"});
anty1_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty1_" + ant_number);
//trace("" + anty_1[ant_number].name);
}
if (ant_index[ant_number] == 2){
anty2_ = new anty_2();
anty2_.name = "anty2_" + (ant_number);
anty2_.height = 118;
anty2_.width = 102;
anty2_.x = 100;
anty2_.y = 300;
addChild(anty2_);
Tweener.addTween(anty2_, {x: 541, time: 3, transition:"linear"});
anty2_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty2_" + ant_number);
}
if (ant_index[ant_number] == 3){
anty3_ = new anty_3();
anty3_.name = "anty3_" + (ant_number);
anty3_.height = 118;
anty3_.width = 102;
anty3_.x = 100;
anty3_.y = 300;
addChild(anty3_);
Tweener.addTween(anty3_, {x: 541, time: 3, transition:"linear"});
anty3_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
//trace("anty3_" + ant_number);
}
else if (ant_index[ant_number] > 3 && ant_index[ant_number] < 7){
anty4_ = new anty_4();
anty4_.name = "anty4_" + (ant_number);
anty4_.height = 118;
anty4_.width = 102;
anty4_.x = 100;
anty4_.y = 300;
addChild(anty4_);
Tweener.addTween(anty4_, {x: 541, time: 3, transition:"linear"});
anty4_.addEventListener(MouseEvent.MOUSE_DOWN, click_leaf_anty);
//trace("anty4_" + ant_number);
}
ant_temp = ant_number;
if (ant_temp < 9) {
//trace("ant_temp er lavere end 9::: " + ant_temp );
ant_temp++;
}
else if (ant_temp == 9) {
//trace("ant_temp lig med 9::: " + ant_temp );
ant_temp = 0;
}
if (ant_index[ant_temp] == 1){
//removeChild(anty1_);
removeChild(getChildByName("anty1_" + (ant_temp)));
}
if (ant_index[ant_temp] == 2){
//removeChild(anty2_);
removeChild(getChildByName("anty2_" + (ant_temp)));
}
if (ant_index[ant_temp] == 3){
//removeChild(anty3_);
removeChild(getChildByName("anty3_" + (ant_temp)));
}
else if (ant_index[ant_temp] == 4){
//removeChild(anty4_);
removeChild(getChildByName("anty4_" + (ant_temp)));
}
/* if (ant_number == 9) { //resets the ant_number - being the end of the 10th ant
ant_number = 0;
start_timer(); // launches the timer again
trace("Timer resetted");
for (var i:Number=0; i<=9;i++){
if (ant_index[i] == 1){
//removeChild(anty1_);
removeChild(getChildByName("anty1_" + (i)));
}
if (ant_index[i] == 2){
//removeChild(anty2_);
removeChild(getChildByName("anty2_" + (i)));
}
if (ant_index[i] == 3){
//removeChild(anty3_);
removeChild(getChildByName("anty3_" + (i)));
}
else if (ant_index[i] == 4){
//removeChild(anty4_);
removeChild(getChildByName("anty4_" + (i)));
}
}//for loop end
}//ends if=9 reset*/
if (ant_number == 9) { //resets the ant_number at 10th ant, and restarts the timer
ant_number = 0;
reset_timer = true;
start_timer();
}
else {
ant_number++;
}
/*else {
ant_number++;
}*/
}
else if (score >= 20 && screen_number != 2){
stop_game();
trace("14");
}
} //create_ant func end
You can also use:
removeChild( evt.currentTarget as DisplayObject );
Or
var clicked_ant:DisplayObject = evt.currentTarget as DisplayObject;
removeChild( clicked_ant );
Let's say that you've created a Ant class, you could have something like this
private function init():void
{
this.addEventListener( MouseEvent.CLICK , clickHandler );
}
private function clickHandler( event:MouseEvent ):void
{
this.removeEventListener( MouseEvent.CLICK , clickHandler );
if( this.parent != null )
this.parent.removeChild( this );
}

Resources