Is there a way to independently move two objects on a graph?

조회 수: 1 (최근 30일)
Austin Hague
Austin Hague 2015년 11월 10일
답변: Walter Roberson 2015년 11월 11일
Is there a way to have an object constantly moving while other code is being executed.
I am currently working on a basketball game where I would like the hoop to constantly move back and forth using a for loop. Then the ball will shoot when you press a button below is what I have so far. The problem I'm encountering is by nesting the ball for loop inside the hoop for loop, every time the ball integrates through its for loop it pauses the hoop's for loop.
%%Game
game = figure;
set(game,'name','Hoops','numbertitle','off');
clf;
axis([0 10 0 10]);
grid on;
hold on;
ball = plot(1,1,'.','color',[1 .5 0],'MarkerSize',100);
hoop = plot(1,8,'sg','MarkerSize',100);
axis([0 10 0 10]);
while(1)
for j = 1:.02:9
hoop.XData=j;
pause(.00005);
end
for j = 9:-.02:1
hoop.XData=j;
pause(.00005);
end
for i = 1:.050:9
pause(.00025);
a=i*8;
ball.YData=i;
ball.MarkerSize = 100-a;
axis([0 10 0 10]);
end
for i = 9:-.050:7
pause(.00025);
ball.YData=i;
ball.MarkerSize = 100-a;
axis([0 10 0 10]);
end
end
clf('reset');
axis([0 10 0 10]);

답변 (1개)

Walter Roberson
Walter Roberson 2015년 11월 11일
Timer objects.
Note: you cannot actually pause() for less than 0.0001 on any known system. 0.0001 is the timer rate (1 KHz) for the underlying system call for Unix type systems. Windows users generally report not being able to do better than 0.001; on my OS-X system I recently found that I can do between 1 1/4 and 1 1/2 millisecond; see http://uk.mathworks.com/matlabcentral/answers/37716-pause-function-in-matlab-for-1-millisecond#answer_47049

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by