Deleting plotted image from a figure if conditions are met

4 ビュー (過去 30 日間)
Daniel Goh
Daniel Goh 2019 年 12 月 11 日
コメント済み: Daniel Goh 2019 年 12 月 12 日
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 件)

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by