A problem about "don't clear" parameter in "Flip" function

My goal is  to "Flip" a moving object under a unaltered background. Firstly, I "flip" the background with the "don't clear" parameter as 1. Then I "flip" the moving object with "don't clear" parameter as 2 constantly in a "for" loop. However,  the window showed many objects simultaneously. I want to show only one object at one time. How could I do that? 

My psychtoolbox version is 3.0.14 - Flavor: beta. My pc system is win7-64bit.

For better discription my issue, I make an example code. 


[window, rect] = Screen('Openwindow',0,255/2); %%open a window

Screen('FillRect', window,[125 0 0], [0.3*rect(3),0.3*rect(4),0.7*rect(3),0.7*rect(4)],2)%%  a red background

Screen('Flip',window,0,1);%% add the red background to the window

WaitSecs(1);

Screen('FillRect', window,[0 125 0], [0.4*rect(3),0.4*rect(4),0.6*rect(3),0.6*rect(4)],2);% green object

Screen('Flip',window,0,2 );

WaitSecs(1);

Screen('FillRect', window,[125 125 0], [0.45*rect(3),0.45*rect(4),0.55*rect(3),0.55*rect(4)],2);% yellow

Screen('Flip',window,0,2 ); 

WaitSecs(1);

Screen('FillRect', window,[0 0 125], [0.47*rect(3),0.47*rect(4),0.52*rect(3),0.52*rect(4)],2);% blue

Screen('Flip',window,0,2 );

WaitSecs(1); 

sca;


In my comprehension , the window will show only the red background and blue object at last. 

But strangly, it show the red background, green and blue objects. I can't explain it. Why it gives green? Maybe I misunderstand the Flip don't clear parameter. I can't figure it out.

I really hope somebody could help me or give me some advices about the Flip framebuffer.

Thank you.


Your observation is expected, since the framebuffer is not cleared. For "moving" object, I think you have to choose clear buffer. As the help indicates, dontclear is for incremental drawing.
-Xiangrui
as mentioned, the behaviour you are seeing is expected.

if your background is simple, just redraw it in the loop. if the background is complex, then you can pre-draw it as a texture and reload the texture inside the loop instead.