Action Script 2, I want to stop the counting down to 0 - actionscript

I need help with Action Script 2.0
I have this scoreboard system with 2 buttons one to count up to a certain number and the other to count down if i need to.
However, the problem with the count down button.
Count Down button code:
on(release) {
score.text = 0;
score.text = scorebk
_root.scorebk--;
if the score is 9 for example when i count down it adds up 1 and then it counts down normally, also when score is 0 it goes to -1...etc.
I do not want that. how can i stop this button from going to minus? and counts normally.

You just have to check that the value is greater than zero before you decrement:
on(release) {
if(scorebk > 0){
scorebk--;
}
score.text = scorebk;
}

Related

How to increase index within a loop & stop enumerating?

I've ran into a situation where I need a loop, but the code below returns only the very last index value. I need it to return the next value and stop enumerating. To explain better, say I have numbers 0-10, when pressing a button I need the index to increase by one. Now say I keep pressing this button until the index reaches 10, on the next press, the index needs to be back at zero (aka cycle through.)
Any help would be appreciated & I will most definitely accept the best answer.
Thanks in advance!
//grabs current index in a scrollview
long long currentSelectedIndex = [preview.swipeView currentIndex];
long long totalNumberOfAvailableSlots = [preview.swipeView indexTotal]; //index total is never an absolute value
#autoreleasepool {
//currentSelectedIndex is supposed/always to be zero on first run
for (int i; = (int)currentSelectedIndex; i <= totalNumberOfAvailableSlots; i++){
NSLog(#"loop: %d", i);
if (i == totalNumberOfAvailableSlots){
i = -1;
[preview.swipeView scrollToIndex:i]; //this will cause [preview.swipeView currentIndex] to be updated with "i" value.
}
}
}
As per my comment, the behaviour you seem to be after is to automatically swipe through the contents of swipeView.
Make a timer which calls the swipeToNext method:
myTimer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:#selector(swipeToNext) userInfo:nil repeats:YES];
Then handle the swipe:
- (void)swipeToNext {
if ([preview.swipeView currentIndex] < [preview.swipeView indexTotal]) {
//Can swipe forward
[preview.swipeView scrollToIndex:[preview.swipeView currentIndex]+1];
} else {
//At last index. Need to go to first.
[preview.swipeView scrollToIndex:0];
}
}
sounds like you need currentSelectedIndex to go through this repeating sequence 0, 1, 2, 3, 4,5, 6, 7, 8, 9, 10, 0, 1, ...
within formLoad you need to initialise your counter
currentSelectedIndex = 0
within the button pressed method, increment the counter - but using the modulo operator to return the remainder - in this case of dividing by 11
currentSelectedIndex = (currentSelectedIndex + 1) % 11
in general, you can make a sequence of 0...n like this
currentSelectedIndex = (currentSelectedIndex + 1) % (n + 1)

Game: ScoreNumber does not increase by 1. It increases by 4 every time (sometimes, 10)

I have a game where you bounce on platforms. I want the score to increase by 1 every time you bounce on a platform. However, it keeps increasing by some random number every time (like 10).
In Game.h I have:
int ScoreNumber;
IBOutlet UILabel *ScoreLabel;
-(void)Score;
...
In Game.m I have:
-(void)Score {
ScoreNumber = ScoreNumber +1.0;
ScoreLabel.text = [NSString stringWithFormat:#"%i", ScoreNumber];
}
-(void)PlatformMoving {
if (CGRectIntersectsRect(Bird.frame, Platform1.frame)) {
[self Score];
}
}
...
This should count by 1 every time but it counts by more than 1
If you place a breakpoint in your (void)Score method you'll probably see that its being called multiple times per bounce because the rectangles probably overlap for multiple frames. You could place a check inside the (void)PlatformMoving method to see if the last test failed before calling Score again. This would ensure that Score is never called on 2 consecutive frames and should prevent scoring any more than one point per bounce.

Display updated text on screen in lua

I am using Lua with gideros
I have the text updating in OnEnterFrame method:
count = count + 1
text7 = TextField.new(conf.fontchange, count)
text7:setPosition(conf.dx - conf.width/3, conf.dy - conf.height/3)
text7:setTextColor(0x000ff)
self:addChild(text7)
but this way the next count is just displayed over the earlier one.
If I do
self:removeChild(text7) , the text is not displayed at all. Where should I be removing the last count so that only the updated count is displayed?
It should be:
text7 = TextField.new(conf.fontchange, count)
text7:setPosition(conf.dx - conf.width/3, conf.dy - conf.height/3)
text7:setTextColor(0x000ff)
self:addChild(text7)
And then later inside ENTER_FRAME event:
count = count + 1
text7:setText(count)

limit UIlabel text field in iOS to two characters, reset when 3 or more are entered

I am a beginner learning iOS development.
I am trying to limit the contents of a label marked "Factor" to only allow 2 whole integers between 0 and 99. I also want to force the user to enter at least two digits for every Factor attempt (i.e. "7" would be "07").
The label is not directly edited by the user. There is a number pad that the user can use to enter the Factor directly (just as you would on a calculator or remote control). There is also an Increment and Decrement button that modifies the label text.
I have limited the label so that it will not go above 99 or below 0 when the Increment or decrement buttons are pushed. But I am stuck on how to limit the user from pressing 3 or more numbers and that getting populated in the label text.
Current behavior:
Currently, if I press '9','0','1', that will result in '901'.
My goal would be to press '9','0','1', and see '1'.
In other words, more than 2 characters would never be displayed and once a third number is pushed, it will overwrite the existing two.
Here is what I am doing for my number buttons:
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if (self.numberCheck)
{ self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];
}
else
{
self.fDisp.text = number;
self.numberCheck = YES;
}
}
I think is is easy to accomplish that. See my solution below, I think it should work in your case.
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text stringByAppendingString:number];
}
You could simply check the length of the label string.
if ([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];

Average of textfields depending on how many textfield are "used"

Little explication: I have an app with 5 textfields, i already got the code to make the avaerage of them. Using floates and giving a variable to each textfield.so, the adding of the value of all the textfield /5 (divided per 5)
My problem is that when a textfield is leaved emptty, its not goona be /5, if user fill in only 4 cells it will be/4 and so on.
Question: how to divide the adding of cells depending in how many cells have content?
I was tryng with textfield.text.length > 0 in an if. But i dont get it
Thanks, hope i was clear.
You could check each textfield and see if the text is NULL or blank (#"")
Use an int to keep track of the number of textfields that have a value.
For example:
int counter = 0;
float total = 0.0;
if([textfield1.text length] > 0)
{
counter++;
float thisValue = [textfield1.text floatValue];
total = total + thisValue;
}
repeat this for all 5 textfields and then divide your total by the counter variable.
You could include the if statement in a loop if you are going to vary the number of text fields or to reduce the amount of coding you do.
EDIT I have changed my answer based on the good suggestions of other to use text.length
try to count the number of used textfields by increasing a number every time you read out a textfield that has valid text in it and divide the result by that number. (logically the "number" should be initialized as 0)

Resources