I am trying to create a simple game.
What it should do: Create 2 pushbuttons and when user clicks each button it should disappear.
What it actually does: When I click the first button it disappears. But when I click the 2nd one nothing happens.
clear all, clc, close all
fh = figure;
n = 2;
x = ceil(rand(10)*2);
y = ceil(rand(10)*2);
bgh = uibuttongroup('Parent',fh,'Title',...
'Button Game','Position',[.1 .2 .8 .6]);
for i = 1:n
rbh1 = uicontrol(bgh,'Style','Pushbutton','String','Red',...
'Units','normalized','Position',[rand(1) rand(1) x(1,i) y(1,i)]);
set(rbh1,'CallBack','set(rbh1,''visible'',''off'')')
end
axt = axes('Parent',bgh,'Units','normalized');
axis([0.5 1 0.5 1])
axis square
axis off
How can I fix this?
The problem is that you’re setting the callback for one handle only. Change the loop bit of your code to the following and it will work. Since this seems like a learning exercise, I’ll leave it to you to explore it and figure out why making this change helps.