how to get keyboard input to move things ?
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi ppl,
I want to get keyboard input so that i can move my player to that location. the code i wrote is not moving the ball. here is the code, can any body tell what is the error in my code.
function [ ball ] = move( ball )
%MOVE Summary of this function goes here
% Detailed explanation goes here
set(ball.fig, 'KeyPressFcn', 'a = get(gcf, ''CurrentCharacter'')');
a = 1;
if double(a) == 28 %left arrow
ball.manMid(1) = ball.manMid(1) - 5;
elseif double(a) == 29 %right arrow
ball.manMid(1) = ball.manMid(1) + 5;
end
set(ball.man,'Position',[ball.manMid, 2*ball.manRad 2*ball.manRad]);
end
and also i want my player to fire when spacebar is pressed, how can i do that.
Thank You :)
댓글 수: 0
답변 (2개)
Daniel Shub
2012년 4월 22일
Graphics objects are not automatically refreshed. You might need to use drawnow.
doc drawnow
댓글 수: 2
Daniel Shub
2012년 4월 24일
I am pretty confident I could write the code, but that really isn't the point of Answers.
Jan
2012년 4월 23일
The code is very unclear. Where is this function called from? It is not a callback, which is triggered by pressing a key. Setting the KeyPressFcn inside the function is unexpected. Then it is strange, that the callback function assigns a in the base-workspace. This is not directly accessible from inside the function.
a = 1;
if double(a) == 28
This is confusing also. Do you expect, that the callback function for KeyPress modifies a and restarts the function in the if double(a) == 28 line?
Currently it is hard to improve your function, because it can only be guessed, what you want to achieve. I suggest to use your favorite search engine to look for other KeyPressFcn examples in the net.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!