I apologize in advance because I am very new to swift. I have an array of strings (answerSet) that pop up randomly and without repeating when a view controller is tapped. I did this through a while loop and added an if statement so once the last string in the array pops up the user can tap the screen and it triggers a segue to go to the next view controller (toEnd). This doesn't work properly as a couple strings occur when the screen is tapped but then the segue is triggered before all the strings in the array have been shown. I have provided the code. Please let me know how to fix this and thanks so much! PM.pngmy code
You need to remove the element in answerSet rather than setting it to an empty string.
answerSet.remove(at: randomIndex)
Without this, it can trigger the segue if on the next loop the randomIndex gets an empty string.
Related
I have an array to store true answers and false answer of random frame multiple choices questions:
var arraytruefalseanswer=[];
I use push method to insert every true and false answer in the array:
arraytruefalseanswer.push(trueanswer)
arraytruefalseanswer.push(falseanswer)
The problem is:
I can not remove the last element of arraytruefalseanswer .
Because
If I use pop method arraytruefalseanswer.pop(),
it will remove all elements in the array arraytruefalseanswer or bring back to
arraytruefalseanswer=[]
If i use delete, it is still leaving null.
Please help... how can I remove the last element of arraytruefalseanswer using flash AS3?
Thank you.
The documentation says: When you do a pop(), it returns items that were popped, and the array gets modified as a side effect. Therefore, in order to just remove the last element, you call arraytruefalseanswer.pop(); as is. You can use trace(arraytruefalseanswer) to verify if anything popped. Also check your code flow, it's possible that when you're popping your last element, you think the code reaches the call once but it's not so, and it say pops your entire array so you've left with an empty array. I can't say more without ever seeing your entire block of code where you work eith your truefalseanswer.
Im having a problem on putting my received data(from backendless) to a tableview.
As you can see on the image i have get data from Backendless and i have checked the data with print and it works.
Now i want to put the data to my tableView, but it does not work as you usually do with making an array in the beginning and then just put return.Sub.count.
Any idea how to do it?
You should declare
var Sub = [String]()
underneath your tableView class declaration (At the very top)
Then, in your function,
fetchingAllUsers()
delete the "var" keyword, and you should be fine.
Also, it'd help if you posted all of your code if you need me to be more specific.
I've ran into a weird problem with flash, I have an array of 92 buttons, at first it was all contained in a single array, the buttons up to the first 20th buttons work, the rest don't.
The buttons will take the user to the next scene basically.
So I tried to breakup the array into multiple arrays, so the first array contains the first 20, the second array the 21-40th and so on, the fifth array contains the 81-92 buttons.
The problem now is I will get this error message:
TypeError #1010: A term is undefined and has no properties
and it'll break all the buttons, rendering all the buttons unusable.
Therefore, I commented out the
for (var a=0; a<buttons.length; a++)
{
firstarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
secondarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
thirdarray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
fourtharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
//fiftharray[a].addEventListener(MouseEvent.CLICK,ArraySelectOne);
}
in my button spawn function and the buttons from the first to fourth array works flawlessly well except the fifth, which when clicked, nothing happens.
So I tired to create a new function whereby it was only the fiftharray in it and called the new function in the spawner, same error, breaks everything.
Then I thought was there a button naming issue whereby i mistyped something, I took the button names in the fifth array and pasted them into the start of the fourtharray, replacing what was in it plus commenting out the fiftharray from my script.
The once unworkable buttons (81 to 92) worked, but now (61 to 80) didn't.
I tried combining all the arrays using the comarray, but only the first 20 buttons worked.
So I am wondering if is there a fix or something to solve this problem, much help is appreciated!
There is no need to have multiple arrays, remove them. The last array is obviously shorter than the rest and that's messing up your code -> you are pointing to an index that doesn't exist in your last array.
There is actually no need to have an array. You have 92 buttons, that's a nice bunch. Why not to put it to a movieclip instead? What's the need of the array?
Let's assume you select all your buttons and put them inside of movieclip called buttonsClip. Now you can just use this code, without typing all the instance names out to put them to the array (like the tutorial did it... that may work for 8 buttons, but 92... come on :) ):
import flash.display.MovieClip;
import flash.events.MouseEvent;
for(var i:uint=0; i<buttonsClip.numChildren; i++) {
var b:MovieClip = buttonsClip.getChildAt(i) as MovieClip; //Assuming the buttons are movieclips
b.addEventListener(MouseEvent.CLICK, onClick);
}
function onClick(e:MouseEvent):void {
trace(e.target.name);
}
I know this Question is currently 8 years old, but maybe this will help another person that is searching for the same thing.
I had pretty much the same problem and in my case it was just that I accidently skipped a number while naming all the Symbols in the libary that I wanted to be in this array. I had 42 Symbols but because of this mistake the function to load the array had 43 and obviously the program was confused about that.
Using iOS6's awesome new UICollectionView how am I able to delete all of the UICollectionViewCell objects in a big loop?
Say I've loaded all my data into it already, I hit refresh, I want to delete everything currently in there, then just call my stuff again.
I've found deleteItemsAtIndexPaths which takes an NSArray, so how can I get all items into it?
The proper way of clearing out a UICollectionVew is to simply clear the data source and then reload the collection view.
So if your data source was an array:
self.dataArray = nil;
[self.collectionView reloadData];
Boom, you're cleared out.
Turns out I can use deleteSections and pass a NSIndexSet through to it, making a range of 0,0 and it'll delete the one and only section.
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 0)];
[self.collectionView deleteSections:indexSet];
I could probably just use indexSetWithIndex but when I did my app crashed.
I am trying to add eventListener for each of the balls i created in the screen. I create the balls with for loop and then assigned them to an array. It worked well so far. However, when i tried to add eventListener for each ball in the loop, it gave me phase nil value error. Can you please help me figure it out? Thanks
Here is my code:
function ballListener(event)
if(phase.event=="ended") then
target.event.isvisible=false
end
end
for i=1,10,1 do
a=display.newImage("ball.jpg")
a.x=math.random(10,200)
a.y=math.random(10,200)
a:addEventListener("touch",ballListener)
table.insert(balls,a)
end
For starters, some things in your ballListener function are backwards. It should be "event.phase" and "event.target" because "phase" and "target" are properties of the event. Also isvisible should be isVisible. The end result should look like this:
function ballListener(event)
if(event.phase=="ended") then
event.target.isVisible=false
end
end
I haven't tested the code, so there may be other issues I've missed.
Check out the Corona API.