Flash unidentified property - arrays

For some reason unknown to me it thinks my array pressed keys is not defined. The error is : "Access of undefined property pressedKeys." But the array works fine in my onKeyDown and Up functions.. Any ideas?
Here is the code:
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
var Entities:Array = new Array();
var PressedKeys:Array = new Array();
//Create player ...more code
function onEnterFrame(event:Event):void
{
updateVel();
//...
}
function updateVel()
{
if (pressedKeys[37]) //getting error here
{
// left
player.velX -= player.speed;
}
else if (pressedKeys[65])
{
player.velX -= player.speed;
}
if (pressedKeys[38])
{
// up
player.velY -= player.speed;
}
else if (pressedKeys[87])
{
player.velY -= player.speed;
}
//more code...
}
function onKeyDown(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = true; //works fine here
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
function onKeyUp(event:KeyboardEvent):void
{
PressedKeys[event.keyCode] = false;
trace("Keycode: " + event.keyCode + " is: " + PressedKeys[event.keyCode]);
}
function rand(minNum:Number, maxNum:Number):Number
{
return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

You need to use PressedKeys not pressedKeys - notice the upper/lower case.

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?

Javascript How do i edit and update an object

New to javascript! What if I want to edit and update an object with pre-existed value?
array [];
// this function is for pre-existed value
function object1 ( mytext ) {
this.text = mytext
}
var usertext = new object ("my text to edit");
memArray.push (usertext);
console.log (usertext)
What about edit/update for prompt value?
function object2(){
var text
this.getData = function(){
return{
text: this.text
}
}
this.setData = function (){
this.text = prompt ("Type anything");
}
return this.setData();
}
var user = new addUserObject;
memArray.push(user.getData());
console.log(memArray);
There's a lot of methods but my method is using "for" iterate to the word you want to edit and change it.
var myArray = ["hello" , "hi"];
console.log(myArray);
function addToArray(str){
myArray.push(str);
}
addToArray("yo");
console.log(myArray);
function editArray(){
var word = prompt("Please enter which word you want to change:", "");
if (word != null && word.length > 0){
var found = false;
for (var i = 0; i < myArray.length; i++){
if (word == myArray[i]){
found = true;
var editWord = prompt("You want to change \"" + word + "\" to:", "");
if (editWord != null && editWord.length > 0){
myArray[i] = editWord;
} else {
console.log("You input nothing.");
}
break;
}
}
if (!found){
console.log("Cannot find this word");
}
} else {
console.log("You input nothing.");
}
}
editArray();
console.log(myArray);

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();
}
});
});

accessing array elements in another frame as3

I have created an Array in frame 1 and dynamically create a list of movieclips with it , and I want to create the same Array in frame 3 and those movieclips again if something is true or false.is it possible ? because when I'm trying to do this , I will get this error :
1151: A conflict exists with definition i in namespace internal.
here is my code at frame 1 :
stop();
import flash.display.MovieClip;
var p_x:uint = 90;
var p_y:uint = 108;
var my_list:String = "Games,Videos, Medias,Images,Photos,Personal Photos,Social Media,Private,Social,None,Names,Families";
var myListString:Array = my_list.split(",");
var myArray:Array=new Array ();
var listObject = 1;
for (var i:uint=0; i<12; i++)
{
var myItemList:mrb=new mrb();
myItemList.x = p_x;
myItemList.y = p_y + 80;
myItemList.y = (p_y + (listObject * 65));
myArray.push(myItemList);
myItemList.txt.text = i.toString();
myItemList.txt.text = myListString[i].toString();
myItemList.name = "item"+i;
addChild(myItemList) as MovieClip ;
listObject++;
}
and here is code at frame 3 :
var tmpCurFrame:int = currentFrame;
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) {
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
if (so.data.fav1 != undefined)
{
if ( so.data.fav1 == "on")
{
for (var i:int = 0; i < myListString.length;)
{ if (myListString[i].indexOf() > -1)
{
var myRectangle:mrb=new mrb();
trace("found it at index: " + i);
myRectangle.x = p_x;
myRectangle.y = (p_y + (findobject * 50));
trace('p_y+80 =' + (p_y+(findobject*80)) + 'p_x = ' + p_x );
myArray.push(myRectangle);
myRectangle.txt.text = myListString[i].toString();
trace(my_array2[i].toString() );
addChild(myRectangle);
}
}
}
}
else
{
fav_1.fav_on.visible=true;
}
}
This error message simply means that you use twice the same variable i. You just have to give them differents names.

TypeError: Error #1010: A term is undefined and has no properties. Problems with successfully removing objects

I am trying to make a game involving a dragon burning little knights, fairly simple but I am new to AS3 and can't seem to solve this problem, sometimes when I kill a knight it returns an output error saying:
TypeError: Error #1010: A term is undefined and has no properties.
at drogtor/onTick()
I can't find any relevant solutions elsewhere on this website so I am submitting a question myself.
Here is (what I think to be) the relevant code.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import global;
import flash.display.DisplayObject;
public class drogtor extends MovieClip
{
private var updown:Boolean=true;
private var fArray:Array;
private var eArray:Array;
var wpressed:Boolean = false;
var apressed:Boolean = false;
var dpressed:Boolean = false;
var spressed:Boolean = false;
var kspawn:Number = 5;
public function drogtor()
{
addEventListener(Event.ENTER_FRAME, onTick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fliy);
stage.addEventListener(KeyboardEvent.KEY_UP, fliy);
fArray = new Array();
eArray = new Array();
}
public function onTick(e:Event):void
{
var fcount:int=0;
var sdargon:int=10;
var rdargon:int=6;
var Angle:Number = (2 * Math.PI * (dargon.rotation/360));
var dy:Number = sdargon * Math.cos(Angle);
var dx:Number = sdargon * Math.sin(Angle);
HitBox.x = dargon.x;
HitBox.y = dargon.y;
HitBox.rotation = dargon.rotation;
//Flame Spewer
if (global.count==9)
{
var fAngle:Number = (2 * Math.PI * (dargon.rotation/360));
var fdy:Number = 10 * Math.cos(fAngle);
var fdx:Number = 10 * Math.sin(fAngle);
var ftemp:Flame=new Flame;
ftemp.x=dargon.x + (10 * fdx);
ftemp.y=dargon.y - (10 * fdy);
ftemp.rotation=dargon.rotation + Math.random() * (15-(-15)) + (-15);
fArray.push(ftemp);
addChildAt(ftemp, 0);
}
var kgen:int = Math.floor((Math.random() * 3)+1);
var stageside:int = Math.floor((Math.random() * 4)+1)
if (kgen == 1)
{
if (Math.floor((Math.random() * 100) + 1) <= kspawn)
{
var ktemp:keniget = new keniget;
if (stageside == 1)
{
ktemp.x = Math.random() * stage.stageWidth;
ktemp.y = 0
}
else if (stageside == 2)
{
ktemp.x = Math.random() * stage.stageWidth;
ktemp.y = stage.stageHeight;
}
else if (stageside == 3)
{
ktemp.x = 0;
ktemp.y = Math.random() * stage.stageHeight;
}
else if (stageside == 4)
{
ktemp.x = stage.stageWidth;
ktemp.y = Math.random() * stage.stageHeight;
}
eArray.push(ktemp);
addChildAt(ktemp, 0);
}
}
for (var iKl:int = eArray.length-1; iKl >= 0; iKl --)
{
var krotation:Number = (Math.atan2(eArray[iKl].y-dargon.y, eArray[iKl].x-dargon.x) * 180/Math.PI) - 90;
eArray[iKl].rotation = krotation
var kangle:Number = (2 * Math.PI * (eArray[iKl].rotation/360));
var edx:Number = 3 * Math.sin(kangle);
var edy:Number = 3 * Math.cos(kangle);
eArray[iKl].x += edx;
eArray[iKl].y -= edy;
if (eArray[iKl].hitTestObject(HitBox))
{
removeChild(eArray[iKl]);
eArray.splice(iKl, 1);
}
for (var iF:int=fArray.length-1; iF>=0; iF--)
{
if (eArray[iKl].hitTestObject(fArray[iF]))
{
removeChild(eArray[iKl]);
eArray.splice(iKl, 1);
kspawn += 0.5
}
}
}
for(var iFl:int=fArray.length-1; iFl>=0; iFl--)
{
if(fArray[iFl].currentFrame==fArray[iFl].totalFrames)
{
removeChild(fArray[iFl]);
fArray[iFl]=null;
fArray.splice(iFl, 1);
}
}
if(updown)
{
dargon.rotationX-=1;
if(dargon.rotationX == -10)
{
updown = false;
}
}
if(!updown)
{
dargon.rotationX+=1;
if(dargon.rotationX == 10)
{
updown = true;
}
}
//Movement
if(wpressed)
{
dargon.x += dx;
dargon.y -= dy;
}
if(apressed)
{
dargon.rotation -= rdargon;
}
if(dpressed)
{
dargon.rotation += rdargon;
}
if(spressed)
{
dargon.x -= ((1/4) * dx);
dargon.y += ((1/4) * dy)
}
}
public function fliy(ke:KeyboardEvent):void
{
if(ke.type == "keyDown")
{
//key=A
if(ke.keyCode==87)
{
wpressed=true;
}
//Key=A
if(ke.keyCode==65)
{
apressed=true;
}
//Key=D
if(ke.keyCode==68)
{
dpressed=true;
}
if(ke.keyCode==83)
{
spressed=true;
}
}
if(ke.type == "keyUp")
{
//key=A
if(ke.keyCode==87)
{
wpressed=false;
}
//Key=A
if(ke.keyCode==65)
{
apressed=false;
}
//Key=D
if(ke.keyCode==68)
{
dpressed=false;
}
if(ke.keyCode==83)
{
spressed=false;
}
}
}
}
}
The problem is, that even though you removed the knight from the display list, the code is still running in the onTick function, and you are trying to access something that is not existing, I suggest you to check whether your knight's reference is null or not, if not, then run the code, else skip it.
EDIT:
Whenever you null an instance (myInstance = null), you can no longer access to it's properties, functions etc.. and if you try, then you get that error above. To avoid it, check whether your object is null or not.
if(myInstance != null) {
//do your stuff with your instance
}

Resources