Deleting plotted image from a figure if conditions are met

12 views (last 30 days)
Daniel Goh
Daniel Goh on 11 Dec 2019
Commented: Daniel Goh on 12 Dec 2019
Hi, I am trying to make a game where one progresses vertically upwards by bouncing off platforms and the platforms underneath disappear once they are off the screen. I reckon this should be done by deleting them from the plot, but I don't know how to do this. This is my platform generating loop which makes 50 random platforms:
%Generate platform field
xpf = [];
ypf = [];
platformrecord = [];
for n = 1:1:50
hold on
xpf = [xpf randi(512)];
ypf = [ypf randi(30)];
platformcentre = [xpf(n) ypf(n)+70*(n-1)];
pfgen(48,16);
end
where pfgen() draws the platform image out of a random selection of 4 images, as below
function pfgen(x,y)
%generates a platform at specified x and y coordinates.
global platformcentre
n = randi(4);
d = randi(2);
platforms = [".\Assets\art\bin\simpleplatform-48x16-v1.png"...
".\Assets\art\bin\simpleplatform-48x16-v2.png"...
".\Assets\art\bin\simpleplatform-48x16-v3.png"...
".\Assets\art\bin\simpleplatform-48x16-v4.png"
];
if d == 1
[pfimage, map, alphachannel] = imread(platforms(n));
imagesc('XData',[platformcentre(1)-x/2 platformcentre(1)+x/2], ...
'YData',[platformcentre(2)-y/2 platformcentre(2)+y/2], ...
'CData',flip(pfimage,1), ...
'AlphaData',flip(alphachannel,1));
else
[pfimage, map, alphachannel] = imread(platforms(n));
pfimage = flip(pfimage,1);
alphachannel = flip(alphachannel,1);
imagesc('XData',[platformcentre(1)-x/2 platformcentre(1)+x/2], ...
'YData',[platformcentre(2)-y/2 platformcentre(2)+y/2], ...
'CData',flip(pfimage,2), ...
'AlphaData',flip(alphachannel,2));
end
Is there a way to say if the y-coordinate of my platform is less than the lowest y-coordinate visible, to remove said platform from the plot? Thanks.

Accepted Answer

Philippe Lebel
Philippe Lebel on 11 Dec 2019
Edited: Philippe Lebel on 11 Dec 2019
If you get the handle of the plot like so:
h = plot(1,1,'Xr')
you can just delete the handle and the data disapears
delete(h)
like wise for imagesc you can do:
doge = true;
h = imagesc
% wow such image
% very pixel
if doge
delete(h)
end
% such blank
% many empty
  1 Comment
Daniel Goh
Daniel Goh on 12 Dec 2019
Ah, thank you, this works! I did not know that deleting the handle deletes the plot.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!