When I press next question, it doesn't go to the next question

1 次查看(过去 30 天)
I'm making a quiz-like game on the app designer section of matlab, where the user can select easy medium hard, which will display different flags for the user to select one of 3 options. I managed to get the first question to appear, but when I added the 'next question' button, the programme does not want to go the next question. Any help would be much appreciated
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ReturnButton matlab.ui.control.Button
NextQuestionButton matlab.ui.control.Button
option3 matlab.ui.control.Button
option2 matlab.ui.control.Button
option1 matlab.ui.control.Button
Label matlab.ui.control.Label
Image matlab.ui.control.Image
Button3 matlab.ui.control.Button
Button2 matlab.ui.control.Button
TextToUser matlab.ui.control.Label
GeographyQuizLabel matlab.ui.control.Label
Button1 matlab.ui.control.Button
end
properties (Access = private)
Property % Description
counter = 1;
Difficulty = [1,2,3];
end
% Button pushed function: Button1
function Button1Pushed(app, event)
% starts easy games
app.Difficulty = 1;
app.counter = app.counter+1;
app.Label.Text = "What is the country seen below?";
app.Button1.Visible = "off";
app.Button2.Visible = "off";
app.Button3.Visible = "off";
app.TextToUser.Text = "";
app.option1.Visible = "on";
app.option2.Visible = "on";
app.option3.Visible = "on";
app.Image.Visible = "on";
if app.counter == 2
app.Image.ImageSource = "italyFlag.svg.png";
app.option1.Text = "Italy";
app.option2.Text = "France";
app.option3.Text = "Spain";
end
% Button pushed function: NextQuestionButton % The counter should increase by 1 when the next
function NextQuestionButtonPushed(app, event)
app.counter = app.counter + 1;
if app.Difficulty == 1 && app.counter == 3
app.Image.ImageSource = "RomaniaFlag.png";
app.option1.Text = "Chad";
app.option2.Text = "Bolivia";
app.option3.Text = "Romania";
end
  1 个评论
Walter Roberson
Walter Roberson 2024-3-22
Your functions Button1Pushed and NextQuestionButtonPushed are not configured as button pushed callbacks. The functions exist but there is no way to get to them.

请先登录,再进行评论。

回答(1 个)

Ishu
Ishu 2024-4-2
Hi Thomas,
I understand that you are not able to go to next question when you press the "next" button and I am assuming that the functionality of your next button is defined by "NextQuestionButtonPushed" function.
This can be due to the possibility that there may be no callback to the "NextQuestionButtonPushed" function so your next button is not behaving as expected. You can add the callback to these functions in the "startUpFcn" callback to the UIFigure.
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% "Next" button creating code
app.SubmitButton.ButtonPushedFcn = createCallbackFcn(app, @NextQuestionButtonPushed, true);
%Similarly you can add callback to "Button1Pushed" also
app.SubmitButton.ButtonPushedFcn = createCallbackFcn(app, @Button1Pushed, true);
end
end
You can add callbacks according to the behaviour of your code.
For more information you can refer below documentation:
Hope it helps.

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by