MATLAB GUI: How do I use pushbuttons to increment a variable in the opening function? (cursor movement)

4 views (last 30 days)
Hello,
I am creating a game in MATLAB using guide for a school project and am hitting a road block implementing pushbuttons. I know the solution is simple but I am not getting it...
Here is the premise of the game (It is based on the GBA game "Advance Wars"): I am using a 10x15 grid of small square axes to display the "Map". Each axes is loaded with an image based on the map terrain, the unit occupying the space, and whether or not the pane is highlighted by the "cursor" (I have created an image for each possible situation and all the script needs to do is display the correct image).
I have successfully made a function which changes the image of a specified axes. Using this function I can make a cursor "blink" in a specified axes by changing that axes image to its highlighted variation, pausing, and then changing it back to its original image.
The user should be able to move the cursor to an adjacent axes by clicking on one of four buttons: "Left", "Right", "Up", and "Down".
Unfortunately this is where i am having problems. The cursor just keeps blinking in the same position even though i am pressing the buttons.
If someone could help I would be extremely grateful!
Here is the script in my GUI OpeningFcn, after I have initialized the image in each axes:
if true
% Begin Game
% Values of important game variables initialized:
% Player 1 has the first turn
Turn = 'P1';
% Cursor location initialized to upper left hand corner
handles.C_Loc = [1,1];
% Game set initially to continue
handles.game_cont = 1;
% Player turn initially set to continue
handles.turn_cont = 1;
%
% Cursor movement initially set to zero
handles.C_Move_H = 0;
handles.C_Move_V = 0;
%
% Until the game conditions are met, the following loop will execute:
while (handles.game_cont == 1)
% Reset cursor location based on which player's turn it is
switch Turn
case ('P1')
handles.C_Loc = [7,3];
case ('P2')
handles.C_Loc = [3,13];
end
% Until the player ends their turn, the following loop will execute:
while (handles.turn_cont == 1)
% Cursor location should be changed based on button pushes:
handles.C_Loc(1) = handles.C_Loc(1) + handles.C_Move_V;
handles.C_Loc(2) = handles.C_Loc(2) + handles.C_Move_H;
% Movement should be reset to avoid continuous movement:
handles.C_Move_V = 0;
handles.C_Move_H = 0;
% Current map is a cell array with the same dimensions as map.
% Each cell holds a string used to decide which image to load.
Old_Im = Current_Map{handles.C_Loc(1),handles.C_Loc(2)};
% Highlighted image is designated by adding "H" to string
New_Im = strcat(Old_Im,'H');
% User-defined function changes axes to highlighted image
Image_Change(handles.C_Loc(1),handles.C_Loc(2), New_Im, handles)
pause(0.75)
% User-defined function changes axes back to original image
Image_Change(handles.C_Loc(1),handles.C_Loc(2), Old_Im, handles)
end
switch Turn
case ('P1')
Turn = 'P2';
case ('P2')
Turn = 'P1';
end
end
end
Here is one of my pushbutton callbacks(for the "UP" button):
if true
handles.C_Move_V = 1;
guidata(hObject, handles);
end
WHAT AM I DOING WRONG?! Please help...

Answers (2)

Kyle
Kyle on 28 Apr 2013
UPDATE:
Found what i was doing wrong. For whatever reason, I was storing my variables which control cursor movement: handles.C_Move_V and handles.C_Move_H incorrectly. My fix was this:
In my OpeningFcn I initialized the variables into a structure other than handles:
Cursor.Location = [0,0];
Cursor.Movement = [0,0];
guidata(hObject, Cursor);
Then, when retrieving cursor location and movement for input into my "blinking" function I used:
Cursor = guidata(hObject);
Cursor.Location(1) = Cursor.Location(1) + Cursor.Movement(1);
Cursor.Location(2) = Cursor.Location(2) + Cursor.Movement(2);
Cursor.Movement(1) = 0;
Cursor.Movement(2) = 0;
guidata(hObject, Cursor);
Old_Im = Current_Map{Cursor.Location(1),Cursor.Location(2)};
New_Im = strcat(Old_Im,'H');
Image_Change(Cursor.Location(1),Cursor.Location(2), New_Im, handles)
pause(0.75)
Image_Change(Cursor.Location(1),Cursor.Location(2), Old_Im, handles)
The pushbutton callbacks were of the form:
Cursor = guidata(hObject);
Cursor.Movement(2) = Cursor.Movement(2)+1;
guidata(hObject, Cursor);
I suppose i was not storing and calling the variable correctly...

Image Analyst
Image Analyst on 28 Apr 2013
Pushbuttons cannot change anything in the opening function. The Opening function is completely done running by the time the GUI appears and you are able to interact with the buttons.
The only work around is to have the opening function call some other function, say you call it "InitializeStuff", and then your push button callbacks can also call InitializeStuff().
  1 Comment
Kyle
Kyle on 28 Apr 2013
Okay. I think i understand. Thanks for your input!
I believe i solved my problem in a different manner using guidata as mentioned above.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!