Deleting plotted image from a figure if conditions are met

조회 수: 5(최근 30일)
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.

채택된 답변

Philippe Lebel
Philippe Lebel 2019년 12월 11일
편집: Philippe Lebel 2019년 12월 11일
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
Daniel Goh
Daniel Goh 2019년 12월 12일
Ah, thank you, this works! I did not know that deleting the handle deletes the plot.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by