how to get keyboard input to move things ?
8 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Daniel Shub
on 22 Apr 2012
Graphics objects are not automatically refreshed. You might need to use drawnow.
doc drawnow
2 Comments
Daniel Shub
on 24 Apr 2012
I am pretty confident I could write the code, but that really isn't the point of Answers.
Jan
on 23 Apr 2012
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 Comments
See Also
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!