Can you have multiple animations running at the same time?

20 ビュー (過去 30 日間)
Julia Dunn
Julia Dunn 2019 年 12 月 3 日
回答済み: Julia Dunn 2019 年 12 月 3 日
Hello! For a final project I am working on coding something similar to guitar hero. Currently, I have one colored dot that travels to the bottom of the screen (when it hits the bottom, the user is supposed to push a button on the keyboard to make sound). I am wondering if it is possible to have multiple of these dots travel down the screen at the same time, but have the times offset? This is what my figure looks like so far: so the yellow ball travels down the screen, and when it hits the open circle at the bottom you are suppsoed to hit the corresponding note, but I want circles to be running down the lines at the same time (but start at different times), and this is the code I have so far: function lines()
close all
x1=[.5 .5];
y1=[0 1];
x2=[1.5 1.5];
x3=[2.5 2.5];
x4=[3.5 3.5];
x5=[4.5 4.5];
%positions of all lines%
hline1=line(x1,y1,'LineWidth',1);
hline2=line(x2,y1,'LineWidth',1);
hline3=line(x3,y1,'LineWidth',1);
hline4=line(x4,y1,'LineWidth',1);
hline5=line(x5,y1,'LineWidth',1);
%plot lines%
hold on
axis([0 5 0 1]);
%set axes%
ballPos1=[.5 1];
%moving ball in this exampe%
ballPos2=[.5 0];
ballPos3=[1.5 0];
ballPos4=[2.5 0];
ballPos5=[3.5 0];
ballPos6=[4.5 0];
%markerballs at the bottom%
hBall2=plot(ballPos2(1),ballPos2(2),'ko','MarkerSize',13);
hold on
hBall3=plot(ballPos3(1),ballPos3(2),'ko','MarkerSize',13);
hold on
hBall4=plot(ballPos4(1),ballPos4(2),'ko','MarkerSize',13);
hold on
hBall5=plot(ballPos5(1),ballPos5(2),'ko','MarkerSize',13);
hold on
hBall6=plot(ballPos6(1),ballPos6(2),'ko','MarkerSize',13);
hold on
hBall1=plot(ballPos1(1),ballPos1(2),'ko','MarkerSize',13,'MarkerFaceColor','y');
%plots all the different circles%
while ballPos1(2)>=.5
set(hBall1,'XData', ballPos1(1), 'YData', ballPos1(2));
ballPos1(2)=ballPos1(2)-.01;
pause(.03)
end
%moves the yellow ball down the line%
end
I'm looking on suggestions for functions/ commands that could help me do this, or maybe a timer? I'm not totally sure yet. Thank you!

採用された回答

Adam Danz
Adam Danz 2019 年 12 月 3 日
編集済み: Adam Danz 2019 年 12 月 3 日
"I am wondering if it is possible to have multiple dots travel down the screen at the same time, but have the times offset?"
It is possible but you'll need to change the while-loop. The code below is conceptual and shows how the while loop could change and control the advacement of each ball.
Each ball has its own logical flag. When true, the ball advances. When false, the ball does not advance, For each ball, there needs to be a decision on each iteration within the while-loop and the result of that decision is whether or not the flag is true or false on the next iteration.
hBall1=plot(ballPos1(1),ballPos1(2),'ko','MarkerSize',13,'MarkerFaceColor','y');
% I ADDED ANOTHER BALL HERE:
ballPos7=[1.5,1];
hBall7=plot(ballPos7(1),1,'ko','MarkerSize',13,'MarkerFaceColor','g');
% SET INITIAL FLAG; when true, the ball will move.
ball1GO = true;
ball7GO = true;
while ball1GO || ball7GO
% Test to determine advancement
if ballPos1(2)<.5
ball1GO = false;
end
% or ball1GO = ballPos1(2)>=.5;
if ballPos7(2)<.4
ball7GO = false;
end
% or ball7GO = ballPos7(2)>=.4;
% Advance to next position
if ball1GO
set(hBall1,'XData', ballPos1(1), 'YData', ballPos1(2));
ballPos1(2)=ballPos1(2)-.01;
end
if ball7GO
set(hBall7,'XData', ballPos7(1), 'YData', ballPos7(2));
ballPos7(2)=ballPos7(2)-.01;
end
pause(.03)
end
Also, here's no need to call "hold on" so many times. "Hold" is a property of the axes. When it's set to 'on' it stays on until it is turned off.
Your while-loop is a fine method to animate a graphics object but just be aware of this function that can be used as well: an = animatedline(x,y)

その他の回答 (1 件)

Julia Dunn
Julia Dunn 2019 年 12 月 3 日
thank you so much!

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by