- Registriert
- 27.09.11
- Beiträge
- 33
Hallo Leute,
ich bin relativ frisch im Bereich XCode, habe zwar Erfahrungen mit anderen Programmen aber es ist halt doch etwas neues.
Ich bin zufällig in YouTube auf ein Video gestoßen, welches die Erstellung eines Quiz zeigte. Also dachte ich mir, warum probier ich das nicht mal.
Was ich bereits habe ist, einen Start View wo ich mit einem Button in den Game View komme. Dann habe ich einen Code im Internet gefunden, der in etwa für ein Quiz sein soll.
Das Problem das ich habe ist, dass bei diesem Code die Fragen nach der Reihe erscheinen. So wie man sie eingibt, erscheinen sie auch.
Ich hätte gerne versucht, das ganze random zu machen. Da ich jedoch nicht weiterkomme und etwas anstehe im Moment, dachte ich an euch. Vielleicht könnt ihr mir ja helfen das zu realisieren.
Vielen Dank für eure Hilfe....Alex
Im Anhang der Code...ich hoffe ihr könnt es nachvollziehen....ignoriert die angegebenen Fragen, sind nicht von mir^^
-(void)askQuestion
{
// Unhide all the answer buttons.
[answerOnesetHidden:NO];
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];
// Set the time for the timer
time = 10000000.0;
// Go to the next question
questionNumber = questionNumber + 1;
// We get the question from the questionNumber * the row that we look up in the array.
NSInteger row = 0;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 6);
}
// Set the question string, and set the buttons the the answers
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected];
[answerOnesetTitle:[theQuizobjectAtIndex:row+1] forState:UIControlStateNormal];
[answerTwosetTitle:[theQuizobjectAtIndex:row+2] forState:UIControlStateNormal];
[answerThreesetTitle:[theQuizobjectAtIndex:row+3] forState:UIControlStateNormal];
[answerFoursetTitle:[theQuizobjectAtIndex:row+4] forState:UIControlStateNormal];
rightAnswer = [[theQuizobjectAtIndex:row+5] intValue];
// Set theQuestion label to the active question
theQuestion.text = activeQuestion;
// Start the timer for the countdown
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];
[selected release];
[activeQuestion release];
}
-(void)updateScore
{
// If the score is being updated, the question is not live
questionLive = NO;
[timerinvalidate];
// Hide the answers from the previous question
[answerOnesetHidden:YES];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
NSString *scoreUpdate = [[NSString alloc] initWithFormat:@"€%d", myScore];
theScore.text = scoreUpdate;
[scoreUpdate release];
// END THE GAME.
NSInteger endOfQuiz = [theQuiz count];
if((((questionNumber - 1) * 6) + 6) == endOfQuiz)
{
// Game is over.
if(myScore > 0)
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\nNice Game \nYou scored %i!", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
else
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\n You're terrible! \nYou scored %i.", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
theLives.text = @"";
}
else
{
// Give a short rest between questions
time = 1;
// Initialize the timer
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];
}
}
-(void)countDown
{
// Question live counter
if(questionLive==YES)
{
time = time - 1;
if(time == 0)
{
// Loser!
questionLive = NO;
theQuestion.text = @"HAHA now you lost alot of points!";
myScore = myScore - 1000;
[timerinvalidate];
[selfupdateScore];
}
}
// In-between Question counter
else
{
time = time - 1;
if(time == 0)
{
[timerinvalidate];
theLives.text = @"";
[selfaskQuestion];
}
}
if(time < 0)
{
[timerinvalidate];
}
}
- (IBAction)buttonOne
{
if(questionNumber == 0){
// This means that we are at the startup-state
// We need to make the other buttons visible, and start the game.
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];
[selfaskQuestion];
}
else
{
NSInteger theAnswerValue = 1;
[self checkAnswer
int)theAnswerValue];
if(restartGame==YES)
{
// Create a restart game function.
}
}
}
- (IBAction)buttonTwo
{
NSInteger theAnswerValue = 2;
[self checkAnswer
int)theAnswerValue];
}
- (IBAction)buttonThree
{
NSInteger theAnswerValue = 3;
[self checkAnswer
int)theAnswerValue];
}
- (IBAction)buttonFour
{
NSInteger theAnswerValue = 4;
[self checkAnswer
int)theAnswerValue];
}
// Check for the answer (this is not written right, but it runs)
-(void)checkAnswer
int)theAnswerValue
{
if(rightAnswer == theAnswerValue)
{
theQuestion.text = @"Daaamn";
myScore = myScore + 50;
}
else
{
theQuestion.text = @"hahaha!";
myScore = myScore - 50;
}
[selfupdateScore];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
questionLive = NO;
restartGame = NO;
theQuestion.text = @"Are you ready?";
theScore.text = @"";
theLives.text = @"";
questionNumber = 0;
myScore = 0;
myLives = 0;
[answerOnesetTitle:@"Start"forState:UIControlStateNormal];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
[selfloadQuiz];
}
-(void)loadQuiz
{
// This is our forced-loaded array of quiz questions.
// FORMAT IS IMPORTANT!!!!
// 1: Question, 2 3 4 5: Answers 1-4 respectively, 6: The right answer
// THIS IS A TERRIBLE WAY TO DO THIS. I will figure out how to do nested arrays to make this better.
NSArray *quizArray = [[NSArrayalloc] initWithObjects:@"Who is the president in USA?",@"Me",@"Obama",@"George Bush",@"Justin Bieber",@"2",
@"Capital in Norway?", @"Bergen", @"Trondheim", @"Oslo", @"Bærum", @"3",
@"The right answer is 3!", @"41", @"24", @"3", @"9", @"1",
@"Do I have a cat?", @"Yes", @"No", @"No, you have a dog", @"No, you have a flying hamster", @"4",
@"Baba", @"Daba jaba?", @"Laba daba haba?", @"Saba daba gaba?", @"Haba haba?", @"4",
nil];
self.theQuiz = quizArray;
[quizArray release];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[theQuestionrelease];
[theScorerelease];
[theLivesrelease];
[answerOnerelease];
[answerTworelease];
[answerThreerelease];
[answerFourrelease];
[theQuizrelease];
[timerrelease];
[super dealloc];
}
ich bin relativ frisch im Bereich XCode, habe zwar Erfahrungen mit anderen Programmen aber es ist halt doch etwas neues.
Ich bin zufällig in YouTube auf ein Video gestoßen, welches die Erstellung eines Quiz zeigte. Also dachte ich mir, warum probier ich das nicht mal.
Was ich bereits habe ist, einen Start View wo ich mit einem Button in den Game View komme. Dann habe ich einen Code im Internet gefunden, der in etwa für ein Quiz sein soll.
Das Problem das ich habe ist, dass bei diesem Code die Fragen nach der Reihe erscheinen. So wie man sie eingibt, erscheinen sie auch.
Ich hätte gerne versucht, das ganze random zu machen. Da ich jedoch nicht weiterkomme und etwas anstehe im Moment, dachte ich an euch. Vielleicht könnt ihr mir ja helfen das zu realisieren.
Vielen Dank für eure Hilfe....Alex
Im Anhang der Code...ich hoffe ihr könnt es nachvollziehen....ignoriert die angegebenen Fragen, sind nicht von mir^^
-(void)askQuestion
{
// Unhide all the answer buttons.
[answerOnesetHidden:NO];
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];
// Set the time for the timer
time = 10000000.0;
// Go to the next question
questionNumber = questionNumber + 1;
// We get the question from the questionNumber * the row that we look up in the array.
NSInteger row = 0;
if(questionNumber == 1)
{
row = questionNumber - 1;
}
else
{
row = ((questionNumber - 1) * 6);
}
// Set the question string, and set the buttons the the answers
NSString *selected = [theQuiz objectAtIndex:row];
NSString *activeQuestion = [[NSString alloc] initWithFormat:@"%@", selected];
[answerOnesetTitle:[theQuizobjectAtIndex:row+1] forState:UIControlStateNormal];
[answerTwosetTitle:[theQuizobjectAtIndex:row+2] forState:UIControlStateNormal];
[answerThreesetTitle:[theQuizobjectAtIndex:row+3] forState:UIControlStateNormal];
[answerFoursetTitle:[theQuizobjectAtIndex:row+4] forState:UIControlStateNormal];
rightAnswer = [[theQuizobjectAtIndex:row+5] intValue];
// Set theQuestion label to the active question
theQuestion.text = activeQuestion;
// Start the timer for the countdown
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];
[selected release];
[activeQuestion release];
}
-(void)updateScore
{
// If the score is being updated, the question is not live
questionLive = NO;
[timerinvalidate];
// Hide the answers from the previous question
[answerOnesetHidden:YES];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
NSString *scoreUpdate = [[NSString alloc] initWithFormat:@"€%d", myScore];
theScore.text = scoreUpdate;
[scoreUpdate release];
// END THE GAME.
NSInteger endOfQuiz = [theQuiz count];
if((((questionNumber - 1) * 6) + 6) == endOfQuiz)
{
// Game is over.
if(myScore > 0)
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\nNice Game \nYou scored %i!", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
else
{
NSString *finishingStatement = [[NSStringalloc] initWithFormat:@"Game Over!\n You're terrible! \nYou scored %i.", myScore];
theQuestion.text = finishingStatement;
[finishingStatement release];
}
theLives.text = @"";
}
else
{
// Give a short rest between questions
time = 1;
// Initialize the timer
timer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(countDown) userInfo:nilrepeats:YES];
}
}
-(void)countDown
{
// Question live counter
if(questionLive==YES)
{
time = time - 1;
if(time == 0)
{
// Loser!
questionLive = NO;
theQuestion.text = @"HAHA now you lost alot of points!";
myScore = myScore - 1000;
[timerinvalidate];
[selfupdateScore];
}
}
// In-between Question counter
else
{
time = time - 1;
if(time == 0)
{
[timerinvalidate];
theLives.text = @"";
[selfaskQuestion];
}
}
if(time < 0)
{
[timerinvalidate];
}
}
- (IBAction)buttonOne
{
if(questionNumber == 0){
// This means that we are at the startup-state
// We need to make the other buttons visible, and start the game.
[answerTwosetHidden:NO];
[answerThreesetHidden:NO];
[answerFoursetHidden:NO];
[selfaskQuestion];
}
else
{
NSInteger theAnswerValue = 1;
[self checkAnswer

if(restartGame==YES)
{
// Create a restart game function.
}
}
}
- (IBAction)buttonTwo
{
NSInteger theAnswerValue = 2;
[self checkAnswer

}
- (IBAction)buttonThree
{
NSInteger theAnswerValue = 3;
[self checkAnswer

}
- (IBAction)buttonFour
{
NSInteger theAnswerValue = 4;
[self checkAnswer

}
// Check for the answer (this is not written right, but it runs)
-(void)checkAnswer

{
if(rightAnswer == theAnswerValue)
{
theQuestion.text = @"Daaamn";
myScore = myScore + 50;
}
else
{
theQuestion.text = @"hahaha!";
myScore = myScore - 50;
}
[selfupdateScore];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[superviewDidLoad];
questionLive = NO;
restartGame = NO;
theQuestion.text = @"Are you ready?";
theScore.text = @"";
theLives.text = @"";
questionNumber = 0;
myScore = 0;
myLives = 0;
[answerOnesetTitle:@"Start"forState:UIControlStateNormal];
[answerTwosetHidden:YES];
[answerThreesetHidden:YES];
[answerFoursetHidden:YES];
[selfloadQuiz];
}
-(void)loadQuiz
{
// This is our forced-loaded array of quiz questions.
// FORMAT IS IMPORTANT!!!!
// 1: Question, 2 3 4 5: Answers 1-4 respectively, 6: The right answer
// THIS IS A TERRIBLE WAY TO DO THIS. I will figure out how to do nested arrays to make this better.
NSArray *quizArray = [[NSArrayalloc] initWithObjects:@"Who is the president in USA?",@"Me",@"Obama",@"George Bush",@"Justin Bieber",@"2",
@"Capital in Norway?", @"Bergen", @"Trondheim", @"Oslo", @"Bærum", @"3",
@"The right answer is 3!", @"41", @"24", @"3", @"9", @"1",
@"Do I have a cat?", @"Yes", @"No", @"No, you have a dog", @"No, you have a flying hamster", @"4",
@"Baba", @"Daba jaba?", @"Laba daba haba?", @"Saba daba gaba?", @"Haba haba?", @"4",
nil];
self.theQuiz = quizArray;
[quizArray release];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[theQuestionrelease];
[theScorerelease];
[theLivesrelease];
[answerOnerelease];
[answerTworelease];
[answerThreerelease];
[answerFourrelease];
[theQuizrelease];
[timerrelease];
[super dealloc];
}